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.

 채택된 답변

Jan
Jan 2017년 6월 30일
편집: Jan 2017년 6월 30일

0 개 추천

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

Thanks Jan for the answer. You guessed it right. Final A should be, A = [7 1 50; 5 2 50; 1 6 50; 5 1 53; 7 3 53; 2 5 53] Would you please tell which website or book is useful in learning advanced things in Matlab like this?
Jan
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
Thanks Jan. I will go through the links.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터 및 File Exchange에서 Shifting and Sorting Matrices에 대해 자세히 알아보기

태그

질문:

2017년 6월 30일

댓글:

2017년 7월 3일

Community Treasure Hunt

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

Start Hunting!

Translated by