How to eliminate the elements in an array from another array when there are repeated elements in both arrays?

2 ビュー (過去 30 日間)
zhehao.nkd
zhehao.nkd 2022 年 6 月 26 日
編集済み: Matt J 2022 年 6 月 26 日
For example,
A = [ 1,2,2,2,3,4];
B = [1,2,2,3];
What I expect is A eliminate elements from B ( 2 with exact the same number of repeats) to get C = [2,4];
Thanks!

回答 (1 件)

Matt J
Matt J 2022 年 6 月 26 日
編集済み: Matt J 2022 年 6 月 26 日
A = [ 1,2,2,2,3,4];
B = [1,2,2,3];
Au=[unique(A),inf];
reps=max( histcounts(A,Au)-histcounts(B,Au) ,0);
C=repelem(Au(1:end-1),reps)
C = 1×2
2 4

タグ

製品


リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by