# What would be the way to remove duplicates for defined numbers in array?

1 view (last 30 days)
Mantas Vaitonis on 7 Jul 2018
Commented: Mantas Vaitonis on 8 Jul 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?
Mantas Vaitonis on 7 Jul 2018
I forgot to mention that repeated values can not be column-vise, however I went through my algorithm and the logic of this part should be changed. The idea was to detect when signals in two arrays are not connect, one array A is filled randomly with ones and other array B with minus ones, thus i though i would do A*10+B, this is were are those numbers from. Suppose I will close this question. Thanks to all for their time trying to answer this question.

Paolo on 7 Jul 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
Mantas Vaitonis on 8 Jul 2018
It works, thank you.