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

1 回表示 (過去 30 日間)
Joe Riddle
Joe Riddle 2013 年 6 月 4 日
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 件のコメント
Azzi Abdelmalek
Azzi Abdelmalek 2013 年 6 月 4 日
Can you provide a small example?
Joe Riddle
Joe Riddle 2013 年 6 月 6 日
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.

サインインしてコメントする。

採用された回答

Roger Stafford
Roger Stafford 2013 年 6 月 6 日
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 件のコメント
Joe Riddle
Joe Riddle 2013 年 6 月 6 日
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 2013 年 6 月 6 日
That was it! I rounded all the "y" coordinates before sorting, and it now works great. Thank you!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeShifting and Sorting Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by