1
votes

I have a vector, let say as=[1 3 4] and I have 30 by 30 cell array. I want to check if the elements of vector as intersect with the elements of each cell or not? If so, I want to find the indices of the cell.

1
Is each cell of the cell array a 1 x 3 numeric array? - Divakar
yes, it is a numerical array - joanna
And by intersect, don't you mean being equal instead? Or else how would you define intersect? - Divakar
I mean by intersect is that if there is at least one element in the vector (as) exist in the cell, no need for both to be equal - joanna

1 Answers

1
votes

Assuming cellarr to be input cell array, see if this approach works for you -

%// Find intersecting elements for each cell
int_idx = cellfun(@(x) intersect(x,as),cellarr,'UniformOutput', false)

%// Find non empty cells that denote intersecting cells.
%// Then, find their row and column indices
[row_ind,col_ind] = find(~cellfun('isempty',int_idx))

Another approach with ismember to find matches among each cell and if there is any match within a cell, find the indices of it -

[row_ind,col_ind] =find(cell2mat(cellfun(@(x) any(ismember(x,as)),cellarr,'un', 0)))

And another -

%// Vertically concatenate all numeric array from cells
vertcat_cells = vertcat(cellarr{:})

%// Get all good matches
matches = any(any(bsxfun(@eq,vertcat_cells,permute(as,[1 3 2])),2),3)

%// Reshape matches into the size of cellarr and get indices of matches
[row_ind,col_ind] = find(reshape(matches,size(cellarr)))