Eliminate all non unique rows from a matrix

조회 수: 25 (최근 30일)
Yannick Roth
Yannick Roth 2017년 6월 17일
답변: the cyclist 2017년 6월 18일
Hello,
i've got a problem I don't really know how to solve efficiently:
I have a 2D pointcloud in matrix form: [x-values, y-values]. Matrix dimensions are nx2.
Now I want to make it so that every x- and every y-value only appear once in the entire matrix. So that for example if the 3rd point has x=2 every other point with x=2 is deleted and if the 1st point has y=7 every other point with y=7 is deleted. I could of course use unique(x) and unique(y) but then the results will most likely have different dimensions and also when I delete doublings of a certain x-value I need to delete the whole row so that the corresponding y-value is deleted, too and the other way round.
I hope my explanation was clear enough (not a native speaker) and someone can help me ;)

답변 (2개)

the cyclist
the cyclist 2017년 6월 18일
What would the output be for
x = [2 6;
2 7;
3 7]
If you mean that you want to keep all the rows, because they are all unique, just do
unique(x,'rows')
If not, then you need to clarify the rule. Keep the first row with the 2? Or the first row with the 7?
  댓글 수: 1
Yannick Roth
Yannick Roth 2017년 6월 18일
the output should be [2 6; 3 7] or [2 7] for my program that doesn't matter. It's just important that every x- and every y-value appear only once. If I use unique(x, 'rows') - as far as I understand it - all values the entire row would have to be the same. So in your example no row would be deleted.

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


the cyclist
the cyclist 2017년 6월 18일
Taking into account your comments in my other answer, I think this does what you want.
M = [2 6;
2 7;
3 7;
4 7];
% Remove non-unique first column values
[~,x_idx] = unique(M(:,1));
M2 = M(x_idx,:);
% Then remove non-unique second column values
[~,y_idx] = unique(M2(:,2));
M3 = M2(y_idx,:);
The output you need is in M3.

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by