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

13 views (last 30 days)
Terek Li on 17 Oct 2016
Commented: Adam on 17 Oct 2016
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'.

1 Comment

Guillaume on 17 Oct 2016
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).

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.

Terek Li on 17 Oct 2016
why? is finding doubles inefficient?