Info

此问题已关闭。 请重新打开它进行编辑或回答。

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

1 次查看(过去 30 天)
>> 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

回答(1 个)

Walter Roberson
Walter Roberson 2015-8-29
4:1 is empty. If you want to count backwards you need 4:-1:1
  2 个评论
Abhishek Prakash
Abhishek Prakash 2015-8-29
编辑:Walter Roberson 2015-8-29
>> 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!

Translated by