Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

determine if array contains specific integer in octave

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

like image 311
Harold Decapia Avatar asked Apr 02 '16 03:04

Harold Decapia


People also ask

How do you find in Matlab?

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 .

How do you check if a number belongs to an array?

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.

What is find in octave?

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.


1 Answers

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
like image 53
Andy Avatar answered Sep 23 '22 14:09

Andy