MATLAB: What would be the way to remove duplicates for defined numbers in array

arrayduplicatesrepeated values

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?

Best Answer

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