sortrows(data, [2,1]) is doing the 2nd round of sorting incorrectly in MATLAB R2017a

Here is some data I'm trying to sort by rows:
-80.0000 5.6250 1.0000
-45.0000 5.6250 1.0000
45.0000 5.6250 1.0000
-65.0000 5.6250 1.0000
25.0000 5.6250 1.0000
-40.0000 5.6250 1.0000
-25.0000 5.6250 1.0000
-15.0000 5.6250 1.0000
-10.0000 5.6250 1.0000
0 5.6250 1.0000
10.0000 5.6250 1.0000
15.0000 5.6250 1.0000
35.0000 5.6250 1.0000
-35.0000 5.6250 1.0000
-30.0000 5.6250 1.0000
-20.0000 5.6250 1.0000
-5.0000 5.6250 1.0000
5.0000 5.6250 1.0000
20.0000 5.6250 1.0000
40.0000 5.6250 1.0000
-55.0000 5.6250 1.0000
30.0000 5.6250 1.0000
55.0000 5.6250 1.0000
65.0000 5.6250 1.0000
80.0000 5.6250 1.0000
If I do
sortrows(data,1);
I get the following desired result:
-80.0000 5.6250 1.0000
-65.0000 5.6250 1.0000
-55.0000 5.6250 1.0000
-45.0000 5.6250 1.0000
-40.0000 5.6250 1.0000
-35.0000 5.6250 1.0000
-30.0000 5.6250 1.0000
-25.0000 5.6250 1.0000
-20.0000 5.6250 1.0000
-15.0000 5.6250 1.0000
-10.0000 5.6250 1.0000
-5.0000 5.6250 1.0000
0 5.6250 1.0000
5.0000 5.6250 1.0000
10.0000 5.6250 1.0000
15.0000 5.6250 1.0000
20.0000 5.6250 1.0000
25.0000 5.6250 1.0000
30.0000 5.6250 1.0000
35.0000 5.6250 1.0000
40.0000 5.6250 1.0000
45.0000 5.6250 1.0000
55.0000 5.6250 1.0000
65.0000 5.6250 1.0000
80.0000 5.6250 1.0000
However, if I do
sortrows(data,[2,1]);
I get the following wrong result:
-80.0000 5.6250 1.0000
-45.0000 5.6250 1.0000
45.0000 5.6250 1.0000
-65.0000 5.6250 1.0000
25.0000 5.6250 1.0000
-40.0000 5.6250 1.0000
-25.0000 5.6250 1.0000
-15.0000 5.6250 1.0000
-10.0000 5.6250 1.0000
0 5.6250 1.0000
10.0000 5.6250 1.0000
15.0000 5.6250 1.0000
35.0000 5.6250 1.0000
-35.0000 5.6250 1.0000
-30.0000 5.6250 1.0000
-20.0000 5.6250 1.0000
-5.0000 5.6250 1.0000
5.0000 5.6250 1.0000
20.0000 5.6250 1.0000
40.0000 5.6250 1.0000
-55.0000 5.6250 1.0000
30.0000 5.6250 1.0000
55.0000 5.6250 1.0000
65.0000 5.6250 1.0000
80.0000 5.6250 1.0000
Can someone please explain why this is happening? Just FYI, I need to use
sortrows(data,[2,1]);
in my code because the data I've shown here is part of a larger chunk where the values in the second column are different.

 Accepted Answer

Nevermind. It's a rounding issue. Doing
round(data,3);
first fixed it.

More Answers (1)

I would hazard the guess that in spite of their appearance using “format short” as you clearly have, the values in the second column are not really all equal. Based on the behavior you show, there are apparently at least five different values present there. Try doing an ‘fprintf’ with %22.20f on that second column and see if I am not right.
for k = 1:25
fprintf(%22.20f\n’,data(k,2))
end

1 Comment

Thanks! I was hasty to post. I should have known it's highly unlikely that something as basic as a sorting function could actually be wrongly programmed. Apologies!

Sign in to comment.

Categories

Products

Tags

Asked:

on 17 Apr 2017

Edited:

on 17 Apr 2017

Community Treasure Hunt

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

Start Hunting!