[英]how to find subitems of a row in cell array matlab?
I have cell array that is as follow: 我的单元格数组如下:
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
because row 1 and row 2 and and row 5 only have subitems of [10,20,30,40,50,60] . 因为第1行和第2行以及第5行只有[10,20,30,40,50,60]的子项。
in my work cell array is big. 在我的工作中,单元格数组很大。 and i need a fast code. 我需要一个快速的代码。
Let 让
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
Then, you can apply ismember
and all
to each cell's contents via cellfun
. 然后,您可以通过cellfun
将ismember
和all
应用于每个单元格的内容。 The result is a logical vector, from which you obtain the desired indices with find
: 结果是一个逻辑向量,从中您可以使用find
获得所需的索引:
result = find(cellfun(@(x) all(ismember(x, t)), S));
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
: 另一种选择(我不知道哪一个在你的情况下会更快)是通过计算与bsxfun
所有成对比较然后应用any
来替换ismember
:
result = find(cellfun(@(x) all(any(bsxfun(@eq, t(:), x(:).'), 1)), S));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.