GAUSS equivalence to Matlab "find" function

I have been struggling to find a GAUSS function that is equivalent to the "find" function in MATLAB. Ideally I would like to have such function to return an index of all elements in a matrix that match certain criterion instead of doing a loop to locate these elements. Could anyone help? Thanks!

1 Answer



0



I think this will accomplish what you are trying to do:

//example data
x = { 0 1 3, 1 0 0, 1 0 0, 2 2 0 };

//convert 'x' to a vector, by
//stacking the columns
x = vec(x);

//set zeros equal to one and non-zeros equal to zero
x_mask = (x .== 0);

//get the indices of the elements that are now zero
idx = indexcat(x_mask, 0);

After the code above, idx will equal:

2
3
4
5
8
9

You can make it much more concise like this:

x = { 0 1 3, 1 0 0, 1 0 0, 2 2 0 };
idx = indexcat((vec(x) .== 0), 0);

or if you wanted to make your own procedure, you could do this:

x = { 0 1 3, 1 0 0, 1 0 0, 2 2 0 };
idx = find(x);

proc (1) = find(x);
   retp(indexcat((vec(x) .== 0), 0));
endp;

aptech

1,773

Your Answer

1 Answer

0

I think this will accomplish what you are trying to do:

//example data
x = { 0 1 3, 1 0 0, 1 0 0, 2 2 0 };

//convert 'x' to a vector, by
//stacking the columns
x = vec(x);

//set zeros equal to one and non-zeros equal to zero
x_mask = (x .== 0);

//get the indices of the elements that are now zero
idx = indexcat(x_mask, 0);

After the code above, idx will equal:

2
3
4
5
8
9

You can make it much more concise like this:

x = { 0 1 3, 1 0 0, 1 0 0, 2 2 0 };
idx = indexcat((vec(x) .== 0), 0);

or if you wanted to make your own procedure, you could do this:

x = { 0 1 3, 1 0 0, 1 0 0, 2 2 0 };
idx = find(x);

proc (1) = find(x);
   retp(indexcat((vec(x) .== 0), 0));
endp;

You must login to post answers.

Have a Specific Question?

Get a real answer from a real person

Need Support?

Get help from our friendly experts.