フィルターのクリア

How to retain only the rows of a matrix based on the unique numbers in the first column of the matrix?

1 回表示 (過去 30 日間)
Hello guys Say that a=[1 3 3; 1 4 3; 2 4 3; 2 5 2; 2 4 2; 3 2 1; 4 3 4; 4 3 2] I need to keep the rows where I have only unique numbers in the first column of the matrix. That means I want to have this matrix a= [1 3 3; 2 4 3; 3 2 1; 4 3 4]. Or I also need to get [1 4 3; 2 4 2; 3 2 1; 4 3 2]. That means retaining the last unique row (based on only the first column) instead of the first. Are they possible? please help.
Kind regards
Sayeed

採用された回答

Azzi Abdelmalek
Azzi Abdelmalek 2014 年 4 月 22 日
編集済み: Azzi Abdelmalek 2014 年 4 月 22 日
[ii,jj,kk]=unique(a(:,1),'stable');
out1=a(jj,:)
out2=cell2mat(accumarray(kk,1:numel(kk),[],@(x) {a(max(x),:)}));
  1 件のコメント
Mohammad Sayeed
Mohammad Sayeed 2014 年 4 月 22 日
Dear Brother It was just perfect! Thank you very much for your kind solution. By applying your codes, I have got both the matrix that I was looking for. Thanks again.
Sayeed

サインインしてコメントする。

その他の回答 (1 件)

Andrei Bobrov
Andrei Bobrov 2014 年 4 月 22 日
編集済み: Andrei Bobrov 2014 年 4 月 22 日
[~,b] = unique(a(:,1),'first');
out1 = a(b,:);
out2 = a([b(2:end)-1;size(a,1)],:);
or
[~,b1] = unique(a(:,1),'first');
[~,b2] = unique(a(:,1),'last');
out1 = a(b1,:);
out2 = a(b2,:);
  1 件のコメント
Mohammad Sayeed
Mohammad Sayeed 2014 年 4 月 23 日
Your codes also worked perfectly brother. Thank you very much for your kind help. Regards
Sayeed

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeMultidimensional Arrays についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by