Find a value in vector

46 ビュー (過去 30 日間)
Liav Daraf
Liav Daraf 2017 年 8 月 31 日
コメント済み: José-Luis 2017 年 8 月 31 日
I have a vector with a 2 million components, how do i find the index of the component which got the value 29.6 in it?

回答 (1 件)

KSSV
KSSV 2017 年 8 月 31 日
idx = find(abs(v-29.6)<=10^-5) ;
where v is your vector with a 2 million components.
  2 件のコメント
Jan
Jan 2017 年 8 月 31 日
Or: <= 1e-10 or what ever limit is matching.
The idea is that comparisons for equality are weak for floating point values. Perhaps the 29.6 is the result of a calculation and is actually a 29.5999999999999. See https://www.mathworks.com/matlabcentral/answers/57444-faq-why-is-0-3-0-2-0-1-not-equal-to-zero
Note that 10^-5 is an expensive power operation , while 1e-5 is a cheap constant. Matlab does not optimize such constant expressions yet.
José-Luis
José-Luis 2017 年 8 月 31 日
Also, this is probably somewhere you want to use eps().
Depending on the order of magnitude of OP's data, @KSSV's solution might yield junk.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeOperators and Elementary Operations についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by