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
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.
채택된 답변
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 Center 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!