如何在单元格数组matlab中找到一行的子项? [英] how to find subitems of a row in cell array matlab?
问题描述
我的单元格如下:
S{1} = [10,20,30,40,50];
S{2} = [10,20,40,50];
S{3} = [10,50,510];
S{4} = [10,20,70,40,60];
S{5} = [20,40];
并且,我需要找到属于以下子项的单元格行:
and, i need to find rows of cell that are subitem of:
[10,20,30,40,50,60]
对于上面的示例结果是:
for above example result is :
1,2,5
因为第1行,第2行和第5行仅具有[10,20,30,40,50,60]子项.
because row 1 and row 2 and and row 5 only have subitems of [10,20,30,40,50,60] .
很大.我需要一个快速的代码.
in my work cell array is big. and i need a fast code.
推荐答案
让我们
S{1} = [10,20,30,40,50];
S{2} = [10,20,40,50];
S{3} = [10,50,510];
S{4} = [10,20,70,40,60];
S{5} = [20,40]; % data
t = [10,20,30,40,50,60]; % target values
然后,您可以应用 ismember
和 all
到每个单元格的内容/es.mathworks.com/help/matlab/ref/cellfun.html"rel =" nofollow noreferrer> cellfun
.结果是一个逻辑向量,您可以使用 find
从中获得所需的索引:
Then, you can apply ismember
and all
to each cell's contents via cellfun
. The result is a logical vector, from which you obtain the desired indices with find
:
result = find(cellfun(@(x) all(ismember(x, t)), S));
另一种方法(我不知道哪种情况会更快速)是通过使用 any
:
An alternative (I don't know which one will be faster in your case) is to replace ismember
by computing all pairwise comparisons with bsxfun
and then applying any
:
result = find(cellfun(@(x) all(any(bsxfun(@eq, t(:), x(:).'), 1)), S));
这篇关于如何在单元格数组matlab中找到一行的子项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!