フィルターのクリア

Sorting matrix with specifications for the order

1 回表示 (過去 30 日間)
Jean
Jean 2012 年 11 月 19 日
Hi,
I have a certain matrix with n rows and 3 columns. I want to sort it according to the sums of the squares of its columns.
For example, let's say my matrix is
0 0 1
0 0 2
1 1 1
Since for the second row we have: 0^2+0^2+2^2 = 4, which is larger than the 3rd row: 1^2+1^2+1^2 = 3. In such a case, I want my matrix to look like:
0 0 1
1 1 1
0 0 2
Thank you for your help, it is very appreciated !

回答 (2 件)

Azzi Abdelmalek
Azzi Abdelmalek 2012 年 11 月 19 日
A=[0 0 1;0 0 2;1 1 1];
[~,idx]=sort(sum(A.^2,2))
out=A(idx,:)
  1 件のコメント
Jan
Jan 2012 年 11 月 19 日
This is faster than the arrayfun approach, but of course this matters for larger matrices only.

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


Yowh
Yowh 2012 年 11 月 19 日
Here .. I think this will work ..
A = [0 0 1;0 0 2;1 1 1;0 0 0];
ans = sortrows(A, [3 2 1])

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by