MATLAB Answers

using find function and logical array

52 ビュー (過去 30 日間)
Lorenne
Lorenne 2018 年 6 月 5 日
コメント済み: Lorenne 2018 年 6 月 5 日
If i have a matrix Z=[1 0 1;0 1 0;1 0 1];
>> find(Z==1)
ans =
1
3
5
7
9
>> find(Z(Z==1))
ans =
1
2
3
4
5
Why does the find(Z(Z==1)) produces the above output?

  0 件のコメント

サインイン to comment.

採用された回答

Birdman
Birdman 2018 年 6 月 5 日
編集済み: Birdman 2018 年 6 月 5 日
Because
Z(Z==1)
will produce
1
1
1
1
1
which is a new column vector generated from the initial line. Then, you want to find the Z(Z==1) which is equivalent to Z(Z==1)~=0, therefore you obtain
1
2
3
4
5
which are the indices of all ones in your vector.

  1 件のコメント

Lorenne
Lorenne 2018 年 6 月 5 日
Thanks!

サインイン to comment.

その他の回答 (2 件)

monika shivhare
monika shivhare 2018 年 6 月 5 日
find(M) takes the matrix given in argument M, and returns matrix of indexes in M where value at that index in M is not zero. Z==1 gives a logical matrix of size(Z) showing 1 at index where value of Z is equal to 1.
>> Z==1
ans =
3×3 logical array
1 0 1
0 1 0
1 0 1
logical indexing of Z returns a array of elements of Z where logical value at that index is 1. So, Z(Z==1) gives array of elements of Z where (Z==1) has value 1
>> Z(Z==1)
ans =
1
1
1
1
1
Now, if we execute find(Z(Z==1)), it will return indexes of Z(Z==1) where value of Z(Z==1) is not zero. Value of Z(Z==1) is nonzero at index [1,2,3,4,5]. Hence,
>> find(Z(Z==1))
ans =
1
2
3
4
5

  2 件のコメント

Stephen Cobeldick
Stephen Cobeldick 2018 年 6 月 5 日
+1 nice and clear explanation!
Lorenne
Lorenne 2018 年 6 月 5 日
Thanks!!

サインイン to comment.


Walter Roberson
Walter Roberson 2018 年 6 月 5 日
What else could it produce? Z(Z==1) is logical indexing and returns the elements of Z where Z is 1 -- a vector of values. Because you asked for 1, all the values in that vector are 1. You then find() on that vector of 1's. None of the 1's are 0, and find() returns the locations of all non-zero values... but all of the values are non-zero, so that is all of the locations in that vector that was created by Z(Z==1)

  0 件のコメント

サインイン to comment.

サインイン してこの質問に回答します。

タグ


Translated by