MATLAB: How to check if a cell element exists in an array of cells?

Hello to the stackoverflow community,

I have an array of cells in the Q array, 5520x1, which consists of arrays like this, for example:

K → Q {1}

ans =

 0     3     1    84

etc.

I would really like to know how I could check if an element of an array of cells exists, as indicated above, in Q? Because if it exists, I do not add anything, but if so, I should have added this element to the end of Q. How can this check be performed correctly? Short decisions, if possible, would be more appreciated.

Thanks in advance for your time, Nick

+4
source share
1 answer

I assume that by "element" you mean the whole vector. So, for example, given

Q = {[1 2 3], [4 5]}

[2 4 3], [4 5] .

: new,

alreadyExists = any(cellfun(@(x) isequal(x, new), Q));

:

>> Q = {[1 2 3], [4 5]};
>> alreadyExists = any(cellfun(@(x) isequal(x, [2 4 3]), Q))
alreadyExists =
     0
>> alreadyExists = any(cellfun(@(x) isequal(x, [4 5]), Q))
alreadyExists =
     1
+4

All Articles