7

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

2 Answers 2

10

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
Sign up to request clarification or add additional context in comments.

Comments

2

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));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.