필터 지우기
필터 지우기

How can I find and remove the nonzero duplicates in each column of a matrix

조회 수: 3 (최근 30일)
X is a n-by-n matrix of integers ranging from 0 to n. I want to find nonzero duplicate entries in each column and remove them.
  댓글 수: 2
Image Analyst
Image Analyst 2016년 7월 4일
편집: Image Analyst 2016년 7월 4일
Unless there is exactly the same number of elements to remove in each column, you can't. For example, you can't "remove" 3 elements from column 1 and 8 elements from column 2. Can you give an example of input and output and how you used unique() to try to solve it?
Matt Talebi
Matt Talebi 2016년 7월 6일
편집: per isakson 2016년 7월 6일
The number of duplicates in each column is either 1 or none. Also the duplicate, if exists, is always the same integer as the column number. Example:
X = [ 1 2 3 4 5
2 9 5 3 8
7 5 4 0 1
6 7 3 2 0
3 1 6 7 9 ];
As can be seen in the 3rd column, 3 is a duplicate. Also in my real data set, the first row is always the column number (similar to this example). If it's not too much to ask I want Y to be:
Y = [ 1 2 5 4 5
2 9 4 3 8
7 5 6 0 1
6 7 0 2 0
3 1 0 7 9 ];
where duplicates removed by shifting one element up and adding two zeros at the end to balance the matrix (the order of numbers should be preserved). Otherwise, I think it should be still fine for me to be able to just identify columns with duplicate and then remove them manually. Thank you for your time!

댓글을 달려면 로그인하십시오.

채택된 답변

per isakson
per isakson 2016년 7월 6일
편집: per isakson 2016년 7월 6일
Given
  • "matrix of integers"
  • "the first row is always the column number"
  • "the duplicate, if exists, is always the same integer as the column number"
Try this
X = [ 1 2 3 4 5
2 9 5 3 8
7 5 4 0 1
6 7 3 2 0
3 1 6 7 9 ];
Y = nan( size(X) );
for jj = 1 : size( X, 2)
isdub = X( :, jj ) == jj;
if any( isdub(2:end) )
col = X(:,jj);
col( isdub ) = [];
Y(:,jj) = cat( 1, col, zeros(sum(isdub),1) );
else
Y(:,jj) = X(:,jj);
end
end
result
>> Y
Y =
1 2 5 4 5
2 9 4 3 8
7 5 6 0 1
6 7 0 2 0
3 1 0 7 9
>>
This code trades performance for readability.
&nbsp
Requirement of comment: "modify the codes ... keep the one in the first row and only remove the other one"
Y = nan( size(X) );
for jj = 1 : size( X, 2)
col = X(2:end,jj);
isdub = col == jj;
if any( isdub )
col( isdub ) = [];
Y(:,jj) = cat( 1, jj, col, zeros(sum(isdub),1) );
else
Y(:,jj) = X(:,jj);
end
end
result
>> Y
Y =
1 2 3 4 5
2 9 5 3 8
7 5 4 0 1
6 7 6 2 0
3 1 0 7 9
  댓글 수: 2
Matt Talebi
Matt Talebi 2016년 7월 6일
Thanks Per, it works flawlessly! Just as a minor modification can you possibly modify the codes such that, once a duplicate detected in a column, keep the one in the first row and only remove the other one (to preserve the column numbers).

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by