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

조회 수: 2 (최근 30일)
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
Image Analyst
Image Analyst 2018년 7월 7일
편집: Image Analyst 2018년 7월 7일
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.
Mantas Vaitonis
Mantas Vaitonis 2018년 7월 7일
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
Paolo 2018년 7월 7일
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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by