ismember returns incorrect results with long nx1 cell array

2 views (last 30 days)
Hey,
I have a cell array of strings(<=50000 elements): strings_array = {50000,1}
the values of the strings all come from the following subset: all_poss_values = {'0' '1' '>1' '>10'}
I am using ismember in a loop to build up a logic vector.
for i=1:length(strings_array)
logic_vector = ismember(strings_array{i,1},all_poss_values(or just '1' etc.))
end
For the most part, this approach works. However on some string arrays the ismember function does not return correct results all the time. For example if I want to build a logic vector showing the position of all 1's in the input cell array, ismember will get 95% of the 1's correctly but will miss out on 5%.
Any ideas why this might be happening? The issue is noticed only on certain input arrays which leads me to believe that there is something weird with the formatting of those arrays however all the arrays should contain identical values.
Best Regards.

Answers (1)

Jan
Jan on 6 Mar 2013
Without doubt ismember() works accurately. Therefore I'm convinced, that the bug is in the code you have only paraphrased roughly. Therefore we cannot guess the cause of the problem.
Do you really mean strings_array{i,i}, or strings_array{i}?
When there are only 4 possible symbols but 50'000 strings, it would be much faster to loop over the 4 symbols instead.
Another approach for cross-checking: FEX: CStrAinBP . This can be much faster than ismember, intersect, setdiff etc.
  1 Comment
Andre
Andre on 6 Mar 2013
Thanks for pointing out the typo. I have edited the original post. What do you mean by looping over the symbols instead?
I guess my question really is how to figure out why individual strings in my data make ismember behave erroneously.
For instance ismember(input_string(100),'1') will return 1 while Ismember(input_string(105),'1') will return zero when the input string to be checked is the same in both cases. Pardon the brackets. My work phone does not have braces :-)

Sign in to comment.

Categories

Find more on Characters and Strings in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!