Sort Columns by ascending order while maintaing cell values in place for every row

3 ビュー (過去 30 日間)
Santiago Poretti
Santiago Poretti 2021 年 6 月 3 日
編集済み: Scott MacKenzie 2021 年 6 月 3 日
I have a code that will create a matrix (32xM) with each column being a random permutation of 32.
for k =1:100 %For however many trials (100 in this case)
p(:,k)= randperm(32); %Create a permutation of 32 in each column
end
Is there a way to now order the random permutations in ascending order without losing the order of permuation? To clarify, can I order an entire column to sort based on its first cell value in ascending order? This should result in the random permutations being organized from least to greatest.

回答 (1 件)

Scott MacKenzie
Scott MacKenzie 2021 年 6 月 3 日
編集済み: Scott MacKenzie 2021 年 6 月 3 日
Hmmm, I think this works...
for k =1:100 %For however many trials (100 in this case)
p(:,k)= randperm(32); %Create a permutation of 32 in each column
end
q = sortrows(p',1:32);
q = q'
Here's an example with smaller values to show the sorting result:
for k =1:8
p(:,k)= randperm(5);
end
p
q = sortrows(p',1:5);
q = q'
p =
3 1 5 5 1 2 1 3
4 4 3 3 5 5 4 4
1 5 2 2 4 4 3 2
2 2 4 1 3 3 2 1
5 3 1 4 2 1 5 5
q =
1 1 1 2 3 3 5 5
4 4 5 5 4 4 3 3
3 5 4 4 1 2 2 2
2 2 3 3 2 1 1 4
5 3 2 1 5 5 4 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