How to remove a certain amount of repeating elements from an array
3 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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!
Respuestas (2)
Davide Masiello
el 6 de Feb. de 2022
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];
0 comentarios
Voss
el 6 de Feb. de 2022
Editada: Voss
el 6 de Feb. de 2022
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);
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);
It's not clear from the description what should happen in the second case when [2 2] matches 2 separate times.
0 comentarios
Ver también
Categorías
Más información sobre Logical en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!