Sortrows(A, [2 1[) is doing the 2nd round of sorting incorrectly.

1 visualizzazione (ultimi 30 giorni)
I am trying to organize a large array based on x,y coordinates. I want the result to be sorted (ascending) by y-coordinate, then within each y-coordinate sorted (ascending) by x-coordinate. (These are the coordinates of a bridge model). The data is stored in an excel sheet (linked: https://skydrive.live.com/?cid=a19c139fd96328c1&id=A19C139FD96328C1%21177&action=Share#cid=A19C139FD96328C1&id=A19C139FD96328C1%21109). Code shown below:
%get data
coord=xlsread('coord_data.xlsx','Sheet1', 'A2:C4207');
%sort by y then x
coord_sort=sortrows(coord,[2,1]);
%write back to sheet
xlswrite('coord_data.xlsx', coord_sort,'Sheet1','E2');
The result is that the y's are sorted properly, but within each y-coordinate, the x's are a bit out of order. (If you were to actually run the code with the given excel data, going from cell E34 to E35, there is a jump with those numbers appearing at cell E136 instead).
I've tried different configurations using sortrows (ie I sorted by x then y in two separate commands) the x-sorting went well in that case, but when I subsequently sorted by y, got the same result.
Why might these problems be occurring?
Thank you. Joe Riddle
  2 Commenti
Joe Riddle
Joe Riddle il 6 Giu 2013
Sure. Here is a smaller example:
>> A
A =
1 1 0
1 2 0
2 3 0
9 3 0
7 2 0
6 1 0
4 3 0
5 2 0
>> A1=sortrows(A,[2,1])
A1 =
1 1 0
6 1 0
1 2 0
5 2 0
7 2 0
2 3 0
4 3 0
9 3 0
>> %this is the same as: ...
>> A11=sortrows(A,1)
A11 =
1 1 0
1 2 0
2 3 0
4 3 0
5 2 0
6 1 0
7 2 0
9 3 0
>> A22=sortrows(A11,2)
A22 =
1 1 0
6 1 0
1 2 0
5 2 0
7 2 0
2 3 0
4 3 0
9 3 0
This is the same command used in the code shown in my original post. In this case, it does what it is supposed to: sorts by 2nd column, then by 1st column (while keeping the 2nd column sorted). But for my much larger dataset, it messes up.

Accedi per commentare.

Risposta accettata

Roger Stafford
Roger Stafford il 6 Giu 2013
I see that your coordinates are listed using decimal fraction displays. I suggest a possible explanation for your apparent difficulties with 'sortrows'. Suppose that you have two rows in which the y values look the same in display but aren't exactly the same down in their least bits. Then the larger of these will occur later after sorting regardless of their associated x values. Just reading the results with ordinary display accuracy, it could look as if 'sortrows' "messed up".
Try displaying the apparently offending sections with 'format hex' which gives an exact (though difficult to interpret) reading of a number's value. Or perhaps use 'fprintf' with something like %28.20e format which can detect small differences.
  2 Commenti
Joe Riddle
Joe Riddle il 6 Giu 2013
I will look into this and let you know if it works. Makes sense, though, especially since the data is coming from a SAP2000-VBA interface that sends the data to Matlab. Thanks.
Joe Riddle
Joe Riddle il 6 Giu 2013
That was it! I rounded all the "y" coordinates before sorting, and it now works great. Thank you!

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Shifting and Sorting Matrices in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by