Subhash Singh
Subhash Singh

Reputation: 33

Strange usage of find() in Matlab

Learning the use of find() in Matlab from here and here, I came across the following usage, but could not find the explanation of the functioning of the code.

X = [0.00000   0.00000   0.00000;
     4.24264   0.00000   0.00000;
     8.48528   4.24264   0.00000]

[A(:,1),A(:,2),A(:,3)] = find(X)

This evaluates to :

A =
    2.0000   1.0000   4.2426                                                                                                                              
    3.0000   1.0000   8.4853                                                                                                                              
    3.0000   2.0000   4.2426

The find() function should return a column vector, but how is the matrix A getting initialized without error?

Upvotes: 2

Views: 101

Answers (1)

m.s.
m.s.

Reputation: 16334

According to the documentation:

If you specify three output variables:

[row,col,v] = find(X)

find returns the row and column subscripts of each nonzero element in array X and vector v, which contains the nonzero elements of X.

row =

     2
     3
     3


col =

     1
     1
     2


v =

    4.2426
    8.4853
    4.2426

In your case, these three vectors are assigned to the columns of matrix A.

Upvotes: 4

Related Questions