ordering columns

1 次查看(过去 30 天)
Jules Ray
Jules Ray 2011-10-31
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

采纳的回答

Walter Roberson
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 个评论
Jules Ray
Jules Ray 2011-10-31
Walter: thanx for your answer...
ihave trying but fails in the las part of the script
sortedA = A(sortorder,:);
give this message
??? Subscript indices must either be real positive integers
or logicals.
any idea?
Walter Roberson
Walter Roberson 2011-10-31
Sorry, the line
sortorder = sortrows(sortkeys);
should instead be
[sortedkeys, sortorder] = sortrows(sortkeys);

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Data Type Conversion 的更多信息

标签

产品

Community Treasure Hunt

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

Start Hunting!

Translated by