Info

This question is closed. Reopen it to edit or answer.

I want to arrange the first column of a matrix in ascending order. But my Code is not showing the correct value.

1 view (last 30 days)
>> A = [15 4; 3 -1; 10 78; -9 101];
>> t=A; % Temporary matrix
>> B= zeros([4,2]); % Output Matrix
>> for i=4:1
MAX_NUM = max(A(1:i,1));
[ROW_NUM, COL_NUM] = find(A==MAX_NUM);
B(i,1:2) = A(ROW_NUM,1:2);
A(ROW_NUM,1:2) = A(i,1:2);
i = i-1;
end
>> A=t;
>> disp(B)
0 0
0 0
0 0
0 0

Answers (1)

Walter Roberson
Walter Roberson on 29 Aug 2015
4:1 is empty. If you want to count backwards you need 4:-1:1
  2 Comments
Abhishek Prakash
Abhishek Prakash on 29 Aug 2015
Edited: Walter Roberson on 29 Aug 2015
>> A = [15 4; 3 -1; 10 78; -9 101];
>> t=A; % Temporary matrix
>> B= zeros([4,2]); % Output Matrix
>> for i=4:-1:1
MAX_NUM = max(A(1:i,1));
[ROW_NUM, COL_NUM] = find(A==MAX_NUM);
B(i,1:2) = A(ROW_NUM,1:2);
A(ROW_NUM,1:2) = A(i,1:2);
end
??? Subscripted assignment dimension mismatch.

Community Treasure Hunt

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

Start Hunting!