Location of a value in a matrix
14 ビュー (過去 30 日間)
古いコメントを表示
Say, I have a matrix x = 0:0.5:20. How can I find the exact location (according to the array) of the value of x==9.5? thanks
0 件のコメント
回答 (2 件)
Walter Roberson
2022 年 12 月 6 日
x = 0:0.5:20
find(x == 9.5)
However...
x = 0:0.1:20
find(x == 0.3)
[found, idx] = ismembertol(0.3, x)
It is typically (much more often than not) an error to use == to search for a value in a floating point array, with the exception of comparing something that has been extracted from the array. For example,
y = sin(x);
[maxy, idx_oneway] = max(y)
idx_anotherway = find(y == maxy)
max() and min() and indexing create bit-for-bit identical copies of values (except for weird nan values) so you can be sure that == will work for them. But you should rarely use == to compare non-integers to a list created with the : operator.
3 件のコメント
Walter Roberson
2022 年 12 月 6 日
x2 = 0:0.5:20;
find(x2 == 9.5)
In any case, please re-read my response where I say "Don't DO that" -- and I show what to do instead.
参考
カテゴリ
Help Center および File Exchange で Matrices and Arrays についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!