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'.

댓글 수: 1

Bruno Luong
Bruno Luong 2018년 12월 15일
so in the above example the result should be FALSE right?

댓글을 달려면 로그인하십시오.

 채택된 답변

Bruno Luong
Bruno Luong 2018년 12월 15일

0 개 추천

[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

Thanks! But still one more query. Suppose,
a = [2 3 2 5 2];
b = [2 7 2];
I need to find the missing elements of 'a' in 'b'. Here the elements are one '2', one '3' and one '5'.
Bruno Luong
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
Thanks Bruno Luong !

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

madhan ravi
madhan ravi 2018년 12월 15일
편집: madhan ravi 2018년 12월 15일

0 개 추천

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

댓글 수: 1

Thanks! But still one more query. Suppose,
a = [2 3 2 5 2];
b = [2 7 2];
I need to find the missing elements of 'a' in 'b'. Here the elements are one '2', one '3' and one '5'.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

태그

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by