我有一个向量,让我说as=[1 3 4]
我有30 by 30
个单元格数组。我想检查向量as
的元素是否与每个单元格的元素相交?如果是这样,我想找到单元格的索引。
答案 0 :(得分:1)
假设cellarr
是输入单元格数组,请查看此方法是否适用于您 -
%// 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))
使用ismember
查找每个单元格之间匹配的另一种方法,如果单元格内有any
匹配,请找到它的索引 -
[row_ind,col_ind] =find(cell2mat(cellfun(@(x) any(ismember(x,as)),cellarr,'un', 0)))
另一个 -
%// 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)))