ordering columns
1 次查看(过去 30 天)
显示 更早的评论
dear colleagues, who has save my job at various situations:
i'm dealining with a code in topographic analysis, the requiered assistance is next:
i got a table A
A=
379 700782,872000000 6015755,46600000 700487,673000000 6015992,71900000
121 700487,673000000 6015992,71900000 700563,348000000 6016086,87700000
379 700563,348000000 6016086,87700000 700858,548000000 6015849,62400000
398 700487,673000000 6015992,71900000 700858,548000000 6015849,62400000
398 700782,872000000 6015755,46600000 700563,348000000 6016086,87700000
121 700782,872000000 6015755,46600000 700858,548000000 6015849,62400000
composed by various
the first is g, and the rest are defined as x1, y1, x2, y2
the question is:
i need to order the table rows with the minor values of the pairs of rows x1 and y1 in ascendent order from top to bottom.
conditions:
the entire row is not modifiable, maintaining intact the rows structure, because are pair of coordinates.
so the requested that i have found in the example before is:
121 700487,673000000 6015992,71900000 700563,348000000 6016086,87700000
121 700782,872000000 6015755,46600000 700858,548000000 6015849,62400000
there are two possibilities, beacuse exist and x and an y values¡¡¡¡
what i'm searching for is the coordinate near to the center than the other (of the cartesian plane X=0 and Y=0). In other word, from this two answers select which-one is the more near of the center of the axes x and y (0,0)
i hope u understand the problem.... is a little bit complicated but well redacted.
Greetings from Congo
0 个评论
采纳的回答
Walter Roberson
2011-10-31
sortkeys = [A(:,2).^2+A(:,3).^2];
sortorder = sortrows(sortkeys);
sortedA = A(sortorder,:);
Note that this will sort entirely based upon distance from origin. If you would prefer to sort based upon g primarily and then distance from origin secondly, use
sortkeys = [A(:,1) A(:,2).^2+A(:,3).^2];
I do not bother to take the square root of the distance because doing so takes computation time and does not affect the sorting order.
2 个评论
Walter Roberson
2011-10-31
Sorry, the line
sortorder = sortrows(sortkeys);
should instead be
[sortedkeys, sortorder] = sortrows(sortkeys);
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Type Conversion 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!