Sorting specific rows with a specific column
4 ビュー (過去 30 日間)
古いコメントを表示
I would like to sort first 3 rows of a matrix A with column 2 and then next 3 rows without changing the first three rows again with column 2. eg, A = [1 6 50; 5 2 50; 7 1 50; 2 5 53; 5 1 53; 7 3 53] Later, with same logic, I would like to sort a matrix with 100 rows.
0 件のコメント
採用された回答
Jan
2017 年 6 月 30 日
編集済み: Jan
2017 年 6 月 30 日
The question is not clear. What does "sort with column 2" mean? A guess:
A = [1 6 50; 5 2 50; 7 1 50; 2 5 53; 5 1 53; 7 3 53];
for k = 1:3:size(A, 1)
[dummy, index] = sort(A(k:k+2, 2));
A(k:k+2, :) = A(index + k - 1, :);
end
Please check if this produces the wanted output. If so, explain, if you need more speed.
3 件のコメント
Jan
2017 年 6 月 30 日
I think the best web source for such questions is: https://www.mathworks.com/matlabcentral/answers/ :-)
After reading the Getting Started chapters of the documentation and seeing Matlab's Onramp, it helps do discuss such questions in the forum until you get familar with creating efficient algorithms. See also: https://www.mathworks.com/matlabcentral/answers/8026-best-way-s-to-master-matlab
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Shifting and Sorting Matrices についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!