# ordering columns

2 views (last 30 days)
Jules Ray on 31 Oct 2011
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 on 31 Oct 2011
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 CommentsShowHide 1 older comment
Walter Roberson on 31 Oct 2011
Sorry, the line
sortorder = sortrows(sortkeys);
[sortedkeys, sortorder] = sortrows(sortkeys);