Finding a vector as a subset of an array
26 次查看(过去 30 天)
显示 更早的评论
I have
a = [2 2];
b = [2 3];
How to find that 'b' contains all the elements of 'a' i.e. here two 2's. Here 'b' contains only one '2'.
采纳的回答
Bruno Luong
2018-12-15
[u,~,j] = unique(a(:));
[tf,loc] = ismember(b(:),u);
s = [length(u),1];
result = all(accumarray(j(:),1,s) <= accumarray(loc(tf),1,s))
3 个评论
Bruno Luong
2018-12-16
编辑:Bruno Luong
2018-12-16
a = [2 3 2 5 2 7];
b = [2 7 2 6];
% Same as above, juste decomposing for re-user later
[u,~,j] = unique(a(:));
[tf,loc] = ismember(b(:),u);
s = [length(u),1];
n = accumarray(j(:),1,s)-accumarray(loc(tf),1,s);
anotin = n>0;
bcontaina = all(~anotin)
n = n(anotin);
anotin = u(anotin);
anotin = table(anotin, n)
Returns
bcontaina =
logical
0
anotin =
3×2 table
anotin n
______ _
2 1
3 1
5 1
更多回答(1 个)
madhan ravi
2018-12-15
编辑:madhan ravi
2018-12-15
a = [2 2];
b = [2 3];
idx=ismember(b,a);
all(idx) % if zero then b doesn't contain all the elements of a else vice versa
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!