Sort an Array with sortrows ( ) with two columns

14 Ansichten (letzte 30 Tage)
Patrick Benz
Patrick Benz am 19 Feb. 2021
Bearbeitet: Stephen23 am 5 Mär. 2021
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 Kommentare
Patrick Benz
Patrick Benz am 24 Feb. 2021
Bearbeitet: Patrick Benz am 24 Feb. 2021
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
dpb
dpb am 24 Feb. 2021
"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;

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Stephen23
Stephen23 am 24 Feb. 2021
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 Kommentare
Patrick Benz
Patrick Benz am 5 Mär. 2021
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
Stephen23
Stephen23 am 5 Mär. 2021
Bearbeitet: Stephen23 am 5 Mär. 2021
Perhaps:
Sortierung(:,1) = Auswertung(index,1)

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

the cyclist
the cyclist am 19 Feb. 2021
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.)

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by