I'd like to delete collums that start with a value that already has been in the matrix

조회 수: 3 (최근 30일)
This is the matrix I'm facing;
1 1 1 1 4 5 5 7 14 14 14 14 14 15 15 15 17
4 3 2 1 1 2 1 1 5 4 3 2 1 3 2 1 2
1 1 1 1 0 1 1 1 1 1 1 1 1 3 2 1 2
And this is the matrix I want to become:
1 4 5 7 14 15 17
4 1 2 1 5 3 2
1 0 1 1 1 3 2
So I want to delete the collums, that have a value on the first row that already has passed in the first row... No idea how
It would be great to keep it as simple as possible

답변 (2개)

Titus Edelhofer
Titus Edelhofer 2015년 5월 10일
Hi Benoit,
what about this solution? Assuming your matrix is named A,
[~,idx] = unique(A(1,:));
Anew = A(:, idx);
Titus

Walter Roberson
Walter Roberson 2015년 5월 10일
row1 = A(1,:);
[urow1, row1order] = unique(row1);
[urow1indices, order_reorder] = sort(row1order);
selected_subset = A(:,row1order(order_reorder));
This can be simplified if it is guaranteed that the elements in the first row are in non-decreasing order.
  댓글 수: 2
Titus Edelhofer
Titus Edelhofer 2015년 5월 11일
Note, that in recent versions of MATLAB unique has the parameter setOrder which is sorted per default. No need to sort the order of indices, simply indexing should do.

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

카테고리

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