How to sort a matrix based on one index I have ?

7 ビュー (過去 30 日間)
Alla Abdella
Alla Abdella 2017 年 10 月 22 日
回答済み: Image Analyst 2017 年 10 月 22 日
t= [ 4 2
2 6
6 9]
c= [ 2
3
1]
Now, I want to arrange t according to c index; I need to get t nex sorted based on c :
t= [ 2 6
4 2
6 9]
Here is what I tried:
h=cell(1,1);
h{1,1}=t;
h{1,1}(c)
The answer I get is just one column sorted, but not the total matrix t.

採用された回答

Cedric
Cedric 2017 年 10 月 22 日
編集済み: Cedric 2017 年 10 月 22 日
I guess/hope that you made a mistake when you built your example of sorted t (that seems to be sorted according to c=[2;1;3]). If so, the solution should be:
t_sorted = t(c,:) ;
  2 件のコメント
Alla Abdella
Alla Abdella 2017 年 10 月 22 日
Thank you! It works!!
Cedric
Cedric 2017 年 10 月 22 日
My pleasure!

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

その他の回答 (1 件)

Image Analyst
Image Analyst 2017 年 10 月 22 日
You need to negate c and add 4 if you want the results you gave:
t= [ 4 2
2 6
6 9]
c= [ 2
3
1]
c2 = -c+4
h = t(c2,:)
You'll see:
h =
2 6
4 2
6 9
exactly as you requested. However, I think you may really want Cedric's solution, despite what you actually asked for.

カテゴリ

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