Sort coordinates in two directions at the same time
16 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
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

Risposta accettata
Matt J
il 1 Mag 2023
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 Commenti
Matt J
il 2 Mag 2023
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.
Più risposte (1)
Matt J
il 1 Mag 2023
Modificato: Matt J
il 1 Mag 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 Commenti
James Tursa
il 1 Mag 2023
Modificato: James Tursa
il 1 Mag 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.
Vedere anche
Categorie
Scopri di più su Shifting and Sorting Matrices in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

