Reputation: 385
I have an array which looks like
test = {1,2,3};
I want to determine if an integer belongs in the array. I tried using ismember() and any() but they both return this:
binary operator '==' not implemented for 'cell' by 'scalar' operations
How will I do this? Thanks in advance
Upvotes: 7
Views: 11119
Reputation: 8091
If you want to check if an integer exists in a matrix:
test = [1, 2, 3];
any (test == 2)
ans = 1
But in your question you use a cell array. In this case I would first convert it to a matrix, then do the same:
b = {1,2,3};
any (cell2mat (b) == 2)
ans = 1
Upvotes: 10
Reputation: 321
You're asking about checking if an array has a given integer but you're using a cell. They're quite different.
If you want to stick to cells you can iterate over it like so
test = {1, 2, 3};
number = 2;
hasNumber = false;
for i = 1:size(test,2)
if(test{i} == number)
hasNumber = true;
break;
end
end
For arrays, on the other hand, you could do just this, for example
test = [1, 2, 3];
number = 2;
hasNumber = ~isempty(test(test == number));
Upvotes: 2