Borrar filtros
Borrar filtros

sorting non-exact x/y data into rows and columns, typewriter style

2 visualizaciones (últimos 30 días)
say I have the following two arrays:
x=[1.01 2.01 1.02 2.02];
y=[1.01 1.02 2.01 2.02];
sorting these rows using
xy = table(x',y')
[~, id] = sortrows(xy)
gives the answer of
id = [1;3;2;4];
I'm hoping to obtain the 'typewriter' answer of
id = [3, 4; 1, 2]
or something similar that tells me the third element of the table is the upper left, etc. The eventual goal is to combine elevation .tiff in the correct order to form a coherent dataset from northwest to southeast.
I feel like this should be a simple solution, but I'm delving into loops and logic and feel like I'm missing something easy.
Thanks in advance.
  3 comentarios
oran
oran el 26 de Abr. de 2024
@the cyclist - thanks. You and I are on the same page. I guess what i'm after is for the sorting algorithm to start in the top left (3) then (4), then (1), (2) - as you've depicted in your plot. 'Typewriter' meaning left to right, top to bottom.
I'm looking for a sort rows equivalent of this:
[~, id] = typewriterSort(xy)
giving a solution of:
>> id = [3,4;1,2]
hopefully this is more clear - sorry for the confusion.
oran
oran el 26 de Abr. de 2024
I should add - if the only way to do this is with logical operations and loops I can do that, just figured there might be something I am missing.

Iniciar sesión para comentar.

Respuesta aceptada

Stephen23
Stephen23 el 27 de Abr. de 2024
x = [1.01,2.01,1.02,2.02];
y = [1.01,1.02,2.01,2.02];
m = [x(:),y(:)]; % simpler
[~,id] = sortrows(round(m),[-2,+1])
id = 4x1
3 4 1 2
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
  2 comentarios
Stephen23
Stephen23 el 27 de Abr. de 2024
Editada: Stephen23 el 27 de Abr. de 2024
"I feel like this should be a simple solution, but I'm delving into loops and logic and feel like I'm missing something easy."
There is no "simple solution" because your problem is ill-defined. The cause becomes clearer when we plot with larger fractional values. Here I increased .01=>.2 and .02=>.4:
x = [1.2,2.2,1.4,2.4];
y = [1.2,1.4,2.2,2.4];
axis();
xlim([1,3])
ylim([1,3])
for k = 1:numel(x)
text(x(k),y(k),sprintf("%d",k))
end
You stated that "I'm hoping to obtain the 'typewriter' answer of id = [3, 4; 1, 2]", but we can see that this corresponds to neither an asending nor descending sort in either dimension. The SORTROWS documentation explains why this is significant: "When the first column contains repeated elements, sortrows sorts according to the values in the next column and repeats this behavior for succeeding equal values." In other words, any ties (i.e. equal values) are distinguished by sorting the remaining columns**. However, your data does not have any equal values! Look at your data: all of those values are different, which means SORTROWS will ultimately just be equivalent to sorting by the primary column that you select in whatever order you select. And that will not give you the index order that you want.
The answer I gave you assumes that you want to consider the "similar" values as being equivalent, and clusters them using ROUND into sets of equal values. But how "similar" are the values allowed to be in order to belong to one cluster? Until you define the problem clearly, there is no general solution.
** This basic concept applies to most any sorting algorithm that repeatedly applies stable sorts to a data set.
oran
oran el 1 de Mayo de 2024
Stephen, thank you for the response. Despite appearances, I was at least aware that the problem was ill-defined. The last sentence of the following was what I had missed for the column sorting vector input argument for SORTROWS, next time i'll read more closely:
Column sorting vector, specified as a nonzero integer scalar or vector of nonzero integers. Each specified integer value indicates a column to sort by. Negative integers indicate that the sort order is descending.
Ultimately, if anyone else is trying to sort 3DEP elevation .tiff's in the future, I wound up selecting the .tiff I needed based on other criteria, converting each to the local coordinate system, and then assembling the xyz data for the target area into matrices. I had a pretty strong case of the fridays last friday.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Shifting and Sorting Matrices en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by