claw91
claw91

Reputation: 171

Can you explain what this line does in Matlab

I'm new to Matlab and I'm just stuck with this line of code:

[r,c] = find(abs(fh) <= 2 );

Beware: ironically it was easy for me understanding what the right part of the assignment is.

The left part however (which is maybe the definition of a variable)... I don't know how to search because I have too generic results by googling just square brackets with something inside.

My assumption is this line of code is creating some a matrix with r rows and c columns but r and c are nowhere to be found in the rest of the code.... or maybe it's a simple array with two elements... but it doesn't make much sense to me honestly.

Can you guys help me please?

Upvotes: 0

Views: 71

Answers (2)

Leonardo Mariga
Leonardo Mariga

Reputation: 1162

Take a look in Matlab find() docs.

  • If X is a vector, then find returns a vector with the same orientation as X.

  • If X is a multidimensional array, then find returns a column vector of the linear indices of the result.

  • If X contains no nonzero elements or is empty, then find returns an empty array.

If you call

X = [18 3 1 11; 
     8 10 11 3; 
     9 14 6  1; 
     4 3 15 21 ]

[row,col] = find(X>0 & X<10,3)

You will get:

row = 3×1

     2
     3
     4

col = 3×1

     1
     1
     1

Which represents the index (row number and column number) of each elements that satifies the condition you defined. Since it returns more than 1 value, you can divide the output in two different variables and that is what the left side represents.

Upvotes: 0

Paolo
Paolo

Reputation: 26014

Whenever you see that syntax, it means that the function being called is returning more than one output argument (two in this case).

The best way to learn about the function output arguments is to check the documentation: https://www.mathworks.com/help/matlab/ref/find.html#d120e368337

[row,col] = find(___) returns the row and column subscripts of each nonzero element in array X using any of the input arguments in previous syntaxes.

The output arguments are positional, so r is row, c is col.

Upvotes: 1

Related Questions