how to find index of a cell that contains a numeric value?

5 ビュー (過去 30 日間)
Terek Li
Terek Li 2016 年 10 月 17 日
コメント済み: Adam 2016 年 10 月 17 日
Hi, there are several posts regarding this question, but none of their solutions actually work...
I have a 1000*1 cell containing random double values, how do I find the index of the cell that contains a specific value x?
I tried to do this:
index = find([data(:,1)] == x)
but I get an error saying: Undefined operator '==' for input arguments of type 'cell'.
Please help, thank you!
  1 件のコメント
Guillaume
Guillaume 2016 年 10 月 17 日
Any reason you're using a cell array for storing scalar values? Using a matrix would be a lot simpler since the above code would then work (and matrices are a lot more memory efficient).

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

採用された回答

Adam
Adam 2016 年 10 月 17 日
find( cellfun( @(d) isequal( d, x ), data) );
works theoretically, but you should never be testing for exact equality with doubles if they are not integer-valued. You should instead replace isequal(...) with some function of your own that uses a tolerance for comparison.
  2 件のコメント
Terek Li
Terek Li 2016 年 10 月 17 日
why? is finding doubles inefficient?
Adam
Adam 2016 年 10 月 17 日
It's not a matter of efficiency it is a matter of the level of accuracy with which a value can be represented in a double. Floating point values cannot be 100% accurately represented so what should be the same value, being arrived at by two different mathematical processes can be represented slightly differently - e.g. 1e-10 different from each other, something that as far as makes no difference is equal to us as humans, but will fail an equality test.
You can read up on this anywhere on the internet related to floating point accuracy if you want.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by