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
find (MATLAB Functions) k = find(X) returns the indices of the array X that point to nonzero elements. If none is found, find returns an empty matrix. [i,j] = find(X) returns the row and column indices of the nonzero entries in the matrix X .
The simplest and fastest way to check if an item is present in an array is by using the Array. indexOf() method. This method searches the array for the given value and returns its index. If no item is found, it returns -1.
If two outputs are requested, find returns the row and column indices of nonzero elements of a matrix. For example: [i, j] = find (2 * eye (2)) ⇒ i = [ 1; 2 ] ⇒ j = [ 1; 2 ] If three outputs are requested, find also returns a vector containing the nonzero values.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With