Different output for find(X) and find(X<5)

4 ビュー (過去 30 日間)
Md. Al-Imran Abir
Md. Al-Imran Abir 2022 年 12 月 3 日
編集済み: dpb 2022 年 12 月 4 日
Whereas the command [row, col, val] = find(X) returns the original values of X in val, the command [row, col, val] = find(X>0) returns logical value in val. What is the reason behind such behaviors?
X = [4, 5, 6, 0, -1, 3, 0];
[row, col, val] = find(X)
row = 1×5
1 1 1 1 1
col = 1×5
1 2 3 5 6
val = 1×5
4 5 6 -1 3
[r, c, v] = find(X>0)
r = 1×4
1 1 1 1
c = 1×4
1 2 3 6
v = 1×4 logical array
1 1 1 1

採用された回答

dpb
dpb 2022 年 12 月 3 日
編集済み: dpb 2022 年 12 月 4 日
find returns the values of the input argument referenced by the returned indices into the (temporary) array. In the second case the argument is the logical array of X>0; the function works as if you had written
Y=(X>5);
[r, c, v] = find(Y);
because that is what happens internally; the argument expression is evaluated, then the operations are applied to that. The find documentation says this when reading it literally but leaves the explanation of using it to obtain what you're looking for (or, actually NOT using find() at all for this purpose to the Tips section. It addresses the above with the following
Tips
  • To find array elements that meet a condition, use find in conjunction with a relational expression. For example, find(X<5) returns the linear indices to the elements in X that are less than 5.
  • To directly find the elements in X that satisfy the condition X<5, use X(X<5). Avoid function calls like X(find(X<5)), which unnecessarily use find on a logical matrix.
  • When you execute find with a relational operation like X>1, it is important to remember that the result of the relational operation is a logical matrix of ones and zeros. For example, the command [row,col,v] = find(X>1) returns a column vector of logical 1 (true) values for v.

その他の回答 (3 件)

Matt J
Matt J 2022 年 12 月 3 日
編集済み: Matt J 2022 年 12 月 3 日
Because the argument you've passed to find() is (X>0) which is a logical array. Therefore, the values returned are taken from that:
X = [4, 5, 6, 0, -1, 3, 0];
A=(X>0)
A = 1×7 logical array
1 1 1 0 0 1 0

Muu
Muu 2022 年 12 月 3 日
>> X
X =
4 5 6 0 -1 3 0
>> X>0
ans =
1 1 1 0 0 1 0

Torsten
Torsten 2022 年 12 月 3 日
find(X) lists the array elements of the double array X that are not equal 0.
find(X>0) lists the array elements of the logical array X>0 that are true.

カテゴリ

Help Center および File ExchangeShifting and Sorting Matrices についてさらに検索

タグ

製品


リリース

R2022b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by