Value dependant sort of matrix rows

1 回表示 (過去 30 日間)
Nicolas Douillet
Nicolas Douillet 2020 年 4 月 3 日
コメント済み: Nicolas Douillet 2020 年 4 月 3 日
Hi,
I have a integer matrix which I know each row contain a certain value.
I would like to sort each row of that matrix in the way this value will always be in front / first position of the row.
The other values order doesn't matter.
For example with a constant value equal to 8 :
M8 = [7 8 5;...
8 7 3;...
4 8 3;...
5 8 4];
I would like in return of such a function
sorted_M8 = [8 7 5;...
8 7 3;...
8 4 3;...
8 5 4];
What is the quickest way to do this in Matlab ?
Thank you, best.
Nicolas

採用された回答

David Hill
David Hill 2020 年 4 月 3 日
sorted_M8=M8';
sorted_M8(ismember(M8',8))=M8(:,1);
sorted_M8=sorted_M8';
sorted_M8(:,1)=8;
  3 件のコメント
David Hill
David Hill 2020 年 4 月 3 日
Two lines.
sorted_M8=ismember(M8,8).*M8(:,1)+~ismember(M8,8).*M8;
sorted_M8(:,1)=8;
Nicolas Douillet
Nicolas Douillet 2020 年 4 月 3 日
Yeah good, thanks again. The only thing I try to avoid in general is to mix logical class objects with double class objects in computation.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeShifting and Sorting Matrices についてさらに検索

製品


リリース

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by