在MATLAB中找到向量和单元数组的交集

时间:2014-10-12 12:09:49

标签: matlab cell-array

我有一个向量,让我说as=[1 3 4]我有30 by 30个单元格数组。我想检查向量as的元素是否与每个单元格的元素相交?如果是这样,我想找到单元格的索引。

1 个答案:

答案 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)))