How to remove a certain amount of repeating elements from an array

Say that I am given an array [2 2 2 3 5 6] from a random number generator
I would like to be able to input an array of [2 2 ]; and be left with an array of [2 3 5 6]
I'm unsure as how to accomplish this; almost everything else I've tried will either delete all copies of the inputted number, or simply only delete one of an inputted number.
Any help that could be offered on this would be greatly appreciated!

1 件のコメント

Brendan De Leon
Brendan De Leon 2022 年 2 月 6 日
To more clearly state what I am trying to do
A = [2 2]
B = [2 2 2 3 5 6]
C = [2 3 5 6]
I'm trying to get to C, by removing A from B; no more, no less. Every other method of removal I've seen removes every 2, or only one, despite how many is inputted into the A array

サインインしてコメントする。

回答 (2 件)

Davide Masiello
Davide Masiello 2022 年 2 月 6 日

0 投票

I am not sure I undestand your objective but maybe this could help
A = [2 2 2 3 5 6];
A = unique(A);
Which yields
A = [2 3 5 6];
Voss
Voss 2022 年 2 月 6 日
編集済み: Voss 2022 年 2 月 6 日
Maybe this:
given = [2 2 2 3 5 6];
input = [2 2];
nG = numel(given);
nI = numel(input);
i = nG-nI+1;
while i > 0
if isequal(given(i+(0:nI-1)),input)
given(i+(0:nI-1)) = [];
i = i-nI;
else
i = i-1;
end
end
disp(given);
2 3 5 6
But notice:
given = [2 2 2 2 3 5 6];
input = [2 2];
nG = numel(given);
nI = numel(input);
i = nG-nI+1;
while i > 0
if isequal(given(i+(0:nI-1)),input)
given(i+(0:nI-1)) = [];
i = i-nI;
else
i = i-1;
end
end
disp(given);
3 5 6
It's not clear from the description what should happen in the second case when [2 2] matches 2 separate times.

カテゴリ

ヘルプ センター および File ExchangeMultidimensional Arrays についてさらに検索

製品

リリース

R2020b

タグ

質問済み:

2022 年 2 月 6 日

編集済み:

2022 年 2 月 6 日

Community Treasure Hunt

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

Start Hunting!

Translated by