Sort coordinates in two directions at the same time

Hi,
I'm working with coordinates and I need to sort them increasing in both x- and y-directions a the same time.
I was thinking on something like
[~,order_x] = sort(lon,'ascend');
[~,order_y] = sort(lat,'ascend');
And then using these as index for lon and lat. The problem is that this process changes the real coordinates. On the attached image, the blue dots represents the original locations and the red number the locations that I got when I use both index at the same time. Which are in the wrong locations.
Any idea how can how sort the points?
Thanks

4 commentaires

Please show how you are using both coordinates "at the same time"
Are these vector or arrays that you are dealing with?
The data are vectors. To generate the above numbers on red I'm using the index as:
for i1 = 1:size(lon)
text(lon(order_x(1)),lat(order_y(i1)),num2str(i1),'color','r')
end
Which should be the same as just renaming lon lat as the output from sort.
Matt J
Matt J le 1 Mai 2023
Déplacé(e) : Matt J le 1 Mai 2023
Perhaps this is what you meant to write?
for i1 = 1:size(lon)
text(lon(order_x(i1)),lat(order_y(i1)),num2str(i1),'color','r')
end
user20912
user20912 le 1 Mai 2023
Déplacé(e) : Matt J le 1 Mai 2023
Oh yeah. It was a typo. Sorry for that. Any idea how can sort them together?

Connectez-vous pour commenter.

 Réponse acceptée

load data_coordinates
tf=~isnan(x)&~isnan(y);
[x,y]=deal(x(tf),y(tf));
[~,t]=sort( [lon-x(1),lat-y(1)]*[x(end)-x(1);y(end)-y(1)] );
[lon,lat]=deal(lon(t),lat(t));
H=plot(x,y,lon,lat,'x');
scatlabel(H(2))

3 commentaires

This work but I don't understand why. I know that the key is in the expression you use on the sort function. Could you elaborate on why you use that? Or if you guide I'll be grateful.
Thanks!
You just draw a ray from (x(1),y(1)) to (lon,lat) and project the ray onto the line. The projected length of that ray is the thing we are sorting.
That makes sense. I ended up using your solution. Thanks!

Connectez-vous pour commenter.

Plus de réponses (1)

Matt J
Matt J le 1 Mai 2023
Modifié(e) : Matt J le 1 Mai 2023
It's not possible to sort so that the Nx2 matrix [lat(:),lon(:)] increases monotonically in both columns simultaneously, but you can sort them lexicographically:
tmp=num2cell(sortrows([lat(:),lon(:)]) ,1);
[lat,lon]=deal(tmp{:});

8 commentaires

I see. But using this approach does not work for me, since I need to find a way to sort the data incresing along a line.
But we can see from your plot that the points do not lie along a line.
user20912
user20912 le 1 Mai 2023
Modifié(e) : user20912 le 1 Mai 2023
I'm really sorry. I've expressed myself really poorly. Let me try again:
I have a line, which function I know, and I also have a series of dots near this line. I also have the coordinates of each of these points (see attached image). What I need is to sort the coordinates from these points in both x- and y- directions. Or more simply, if I were to move along the line, which would be the order of the points?
In order to be able to sort the way you want, the data would have to be such that for all (x1,y1) that there is no (x2,y2) such that x2 > x1 but y2 < y1 . Graphically, if you were to scatter() the data, the entire data would have to be between 45 degrees and 90 degrees relative to each proceeding value.
Matt J
Matt J le 1 Mai 2023
Modifié(e) : Matt J le 1 Mai 2023
@user20912 You have the ability here to post .mat files. I suggest attaching a .mat file containing both the point coordinates and the data that describes the given line, so that we can better demonstrate solutions.
Thank for your help.
I've attached a file with the location of the dots and the data that describe the line.
James Tursa
James Tursa le 1 Mai 2023
Modifié(e) : James Tursa le 1 Mai 2023
Would the desired ordering for the above image be 1,2,5,3,6,4,8,7,10,9,... etc.? I.e., pick off the points in order as you move a perpendicular along the line? You could just rotate all the points (e.g. into a vertical line) and then sort by latitude. But since this is all on the surface of a sphere (or spheriod), that begs the question what is really meant by the ordering you want. I.e., spherical trig conversions are probably involved to get what you want in a rigorous fashion. And what exactly is this line? Is it an arbitrary curve, a line on the Mercator projection, or is it really a great circle arc? E.g., if the line was really a great circle arc and passed near the north pole, you couldn't rely on any type of simple ordering or rotations to get the desired result. So, can you describe in more detail what is your actual desired outcome, what exactly is this line, and what range limitations (if any) your data have? Maybe if the data is always clustered in a small area sufficiently away from the poles a simple rotation followed by a sort (or the equivalent) would be good enough for you.
user20912
user20912 le 2 Mai 2023
Modifié(e) : user20912 le 2 Mai 2023
Would the desired ordering for the above image be 1,2,5,3,6,4,8,7,10,9,... etc.?
I.e., pick off the points in order as you move a perpendicular along the line?
You could just rotate all the points (e.g. into a vertical line) and then sort by
latitude.
That is correct. I also think that is better to rotate all the points, but the method I was using, didn't work. About what is the meaning of the line, is just a straight line between two points that are close on Earth; the distance is lower than 1 km. So, all I need is to find a way to sort in the way you describe.
PS: I didn't find the option to cite.

Connectez-vous pour commenter.

Catégories

Produits

Version

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by