Sort an Array with sortrows ( ) with two columns

Hey guys,
I'm sorting an 82x4 arraywith sortrows. I want it to be sorted in a descending way. First the second column shall be significant and for tiebreakers the third column.
My code is:
Knotenpaare=sortrows(Auswertung,[2 3],'descend')
where Auswertung is the array. The first 40 rows of the array are sorted beautifully. But at the moment the values in the second column switch from positive to negative the sorting changes.
Why are the values in the third column now ascending?
Is there a better way to sort these kinds of arrays ?

7 Comments

Stephen23
Stephen23 on 19 Feb 2021
Edited: Stephen23 on 19 Feb 2021
"Why are the values in the third column now ascending?"
Probably because the values in the second column are not the same. They might look the same when displayed to five significant digits, but have you actually compared them (e.g. diff, eq, etc.)?
The Values are exactly the same.
These are coordinates of a mesh and the nodes have the exact same x-coordinate (2. column) and only differ in y-direction (3. column). The bigger value in the third column is the node at the surface and the lower value is underneath the surface.
Shouldn't matter, but try
Knotenpaare=sortrows(Auswertung,[2 3],{'descend','descend'})
just in case.
Attach the section of the data as a .mat file so folks can try to duplicate symptoms
@Patrick Benz: I assume also, that the equally looking numbers -1.0424 are not identical, if you consider all decimal places. Please post the data as MAT file. The 6 relevant rows are enough to finde the difference.
"The Values are exactly the same."
Until we test the actual data we have no reason to think that they are exactly the same values.
Please upload the data in a .mat file by clicking the paperclip button.
I have rechecked the numbers and shame on me.
They aren't exactly the same, although they should. They differ in the 6th digit right of the decimal point.
It is -1.04237926 and -1.04237354.
That is why I tried to "fix" it with
node_Num=round(node_Num(:,1:4),5);
but then the first Value is smaller, because it is getting more negative so to say.
Is there an option to cut the number after 5 digits off? and not only visual.
I have attached the imported node Values as .mat File.
The columns are: node No., x-coordinate, y-coordinate, z-coordinate.
And I want them sorted in that way, that the two nodes with the very similar x-coordinate descending with the y-coordinate beeing the tiebreaker
"Is there an option to cut the number after 5 digits off? and not only visual."
node_Num=sign(node_Num).*floor(abs(node_Num)*1E5)/1E5;

Sign in to comment.

 Accepted Answer

There are probably nicer ways to do this, as this unfortunately changes the data itself. If required, you could use the index output from sortrows to sort the original data matrix.
format long g
S = load('Auswertungsknoten.mat');
A = S.Auswertung
A = 82×4
24 -20.8047 19.15862 0 26 20.8047 19.15862 0 953 19.76845 19.04576 0 954 18.73158 18.93867 0 955 17.69413 18.83735 0 956 16.65614 18.74179 0 957 15.61763 18.65202 0 958 14.57864 18.56802 0 959 13.53919 18.48981 0 960 12.49933 18.41738 0
for k = 1:size(A,2)
[U,X,Y] = uniquetol(A(:,k),1e-3);
A(:,k) = U(Y);
end
A
A = 82×4
24 -20.8047 19.15862 0 24 20.8047 19.15862 0 953 19.76844 19.04576 0 953 18.73156 18.93867 0 953 17.69411 18.83735 0 953 16.65611 18.74179 0 953 15.61759 18.65202 0 953 14.57859 18.56802 0 953 13.53915 18.48981 0 953 12.49928 18.40115 0
B = sortrows(A,[-2,-3])
B = 82×4
24 20.8047 19.15862 0 145416 20.8047 18.90707 0 953 19.76844 19.04576 0 145016 19.76844 18.79436 0 953 18.73156 18.93867 0 144616 18.73156 18.68741 0 953 17.69411 18.83735 0 144216 17.69411 18.56802 0 953 16.65611 18.74179 0 143816 16.65611 18.48981 0
B(38:48,:)
ans = 11×4
138216 2.08471 17.75 0 953 1.04237 17.9845 0 137816 1.04237 17.75 0 953 0 17.9845 0 137416 0 17.75 0 953 -1.04238 17.9845 0 137016 -1.04238 17.75 0 953 -2.08473 18.0116 0 136616 -2.08473 17.75 0 953 -3.12701 18.0116 0

4 Comments

I'm sorry, that I have to unaccept your answer.
I just realized that it is in some case a problem that the original data are altered in that way, that I need the Data in the first column to kind of stay intact.
Hence these are the Node Numbers I need them be correct. When I am using your suggestion the I have multiple times the same node number. Is there a way to use your method to sort the data but keep the node numbers sorted, but not altered?
Or is there a different option to reassign the correct node numbers to the rows afterwards?
"Is there a way to use your method to sort the data but keep the node numbers sorted, but not altered?"
One simple approach is to make a copy of the data and process that. Then use the second output from sortrows (which is the sort index) to sort your original data. That is why I gave that hint in my answer.
"I need the Data in the first column to kind of stay intact."
Then don't apply uniquetol the first column, which you can achieve with a minor change to the for loop:
for k = 2:size(A,2)
% ^ start from the second column
"Or is there a different option to reassign the correct node numbers to the rows afterwards?"
The question is not clear to me.
In theory I understand what you mean. I got the secound output which is the row number of the unsorted Data.
So I can take the row number, grab the Node number regarding to the sort index from the original data and place it in the first column of my sorted data.
So my first Idea would be to solve the problem with a loop.
for i = 1:length(Sortierung)
Sortierung(i,1)=Auswertung(index(i),1)
end
But I have learned that Matlab is more efficient, when using arrays instead of loops.
Is there a better way than the loop?
Sorry for all the questions, but since this will be a major project I'm trying to get better with matlab every day I'm working on it
Perhaps:
Sortierung(:,1) = Auswertung(index,1)

Sign in to comment.

More Answers (1)

See my answer (and other comments) to this very similar question. (As with the comments above, the premise is that the displayed value is not sufficient to see a tiny difference between the numbers.)

Categories

Tags

Asked:

on 19 Feb 2021

Edited:

on 5 Mar 2021

Community Treasure Hunt

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

Start Hunting!