Location of a value in a matrix
조회 수: 17 (최근 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!