Why does the SORTROWS function give incorrect results for complex matrices in MATLAB 7.2 (R2006a)?

1 view (last 30 days)
I am trying to sort my complex matrix with respect to the second column using the SORTROWS function in MATLAB 7.2 as follows:
test=[0:.05:1;0:.05:1]';
test(19,1)=test(19,1)+.2i;
test(19,2)=-test(19,2);
a=sortrows(test,2);
plot(a(:,2))
As seen from the plot, the rows are sorted in incorrect order.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 27 Jun 2009
The documentation for the SORTROWS function mentions the following:
When A is complex, the elements are sorted by magnitude.
The above is true even if the column provided to the SORTROWS function is real. The SORTROWS function cannot be used to sort according to real behavior on a complex matrix.
You may instead, use SORT on the real column, then use the returned indices to manipulate the original matrix as follows:
test=[0:.05:1;0:.05:1]';
test(19,1)=test(19,1)+.2i;
test(19,2)=-test(19,2);
test2=test;
[a,b]=sort(test(:,2))
clear a;
for j = 1:length(test)
test2(j,:) = test(b(j),:);
end
clear test
plot(test2(:,2))

More Answers (0)

Categories

Find more on Shifting and Sorting Matrices in Help Center and File Exchange

Products


Release

R2006a

Community Treasure Hunt

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

Start Hunting!