What would be the way to remove duplicates for defined numbers in array?
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Mantas Vaitonis
el 7 de Jul. de 2018
Comentada: Mantas Vaitonis
el 8 de Jul. de 2018
Hello to All,
I am trying to solve the following situation. There is array IN (NxM), size my vary. My input is
IN=
9 -1 9 -1 -1
9 9 9 9 9
9 10 9 -1 -1
9 9 9 10 9
9 9 10 10 -1
9 -1 9 9 9
-1 9 -1 9 9
-1 -1 9 9 9
Output supposes to look like this:
9 -1 9 -1 -1
9 9 9 9 9
9 10 9 -1 -1
9 9 9 10 9
9 9 10 0 -1
9 -1 9 9 9
-1 9 -1 9 9
0 -1 9 9 9
Only numbers 10 and -1 can not have repeated values, if they are repeated, they should be changed to zero. How could it be achieved?
3 comentarios
Image Analyst
el 7 de Jul. de 2018
Editada: Image Analyst
el 7 de Jul. de 2018
And for the 10 you zero out the second 10 but for the bottom row you zero out the first -1. Have you thought this through? Please read this link to make it easy for us to help you. Please explain the "use case", i.e. why you want to do this so we have some context.
Respuesta aceptada
Paolo
el 7 de Jul. de 2018
You can use strfind for matching patterns in arrays. Read more about it here. (link). The code below defines two patterns, two consecutive -1s and two consecutive 10s.
strfind is used in a loop to search for the pattern in every column. When the pattern is found, the substitution is carried out.
IN= [
9 -1 9 -1 -1
9 9 9 9 9
9 10 9 -1 -1
9 9 9 10 9
9 9 10 10 -1
9 -1 9 9 9
-1 9 -1 9 9
-1 -1 9 9 9];
first_pattern = [-1 -1];
second_pattern = [10 10];
[row,col] = size(IN);
for i = 1:col
IN(strfind(IN(:,i)',first_pattern)+1,i) = 0;
IN(strfind(IN(:,i)',second_pattern)+1,i) = 0;
end
Resulting IN:
IN =
9 -1 9 -1 -1
9 9 9 9 9
9 10 9 -1 -1
9 9 9 10 9
9 9 10 0 -1
9 -1 9 9 9
-1 9 -1 9 9
0 -1 9 9 9
Más respuestas (0)
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!