Sorting rows by column 1, and then sorting each subset of column 1 by column 2... i.e sort so X coordinates are ascending and then sort all values with the same X by Y

2 visualizzazioni (ultimi 30 giorni)
Hi all,
i have a point cloud stored as a 2D matrix, that is arranged as follows:
[X1, Y1, Z1;
X2, Y2, Z2;
X3, Y3, Z3;
X4, Y4, Z4;
X5, Y5, Z5;
.........]
I need to order it in terms of X first so have used sortrows(ARRAY,1)
but i now need to sort the subsiquent array in terms of Y, so for example if 5 X coordinates are the same, it would order them in terms of Y:
-0.9793 19.6374 -0.4139
-0.9793 19.7048 -0.2340
-0.9793 19.7351 -0.4889
-0.9793 19.8851 -0.2104
-0.9793 19.7237 -0.2300
-0.9793 19.6427 -0.4213
-0.9793 19.6195 -0.2687
-0.9793 19.7797 -0.2203
-0.9793 20.1755 -0.3784
-0.9793 19.6788 -0.4635
-0.9792 19.6477 -0.4281
-0.9792 20.1513 -0.4128
-0.9792 19.6024 -0.2839
-0.9792 20.1289 -0.4351
-0.9792 20.1161 -0.2207
-0.9792 20.1566 -0.2360
-0.9792 19.9529 -0.2074
-0.9792 20.1677 -0.3921
-0.9792 20.0403 -0.4777
These would then be ordered in terms of Y, whilst preserving the X coordinates if that makes sense....
I'd appreciate any help!
Regards, Ben

Risposte (1)

Arjun
Arjun il 9 Ago 2024
Hi,
As per my understanding, you want to sort the rows of the array first based on the column X and then on this you want to further sort on the basis of column Y. To address this need MATLAB has a function named sortrows, you can use this function with multiple columns specified. This will ensure that the sorting is hierarchical: first by the X values, and then by the Y values within each group of equal X values.
sortedPointCloud = sortrows(pointCloud, [1, 2]);
%Where column 1 is the X column and column 2 is the Y column
I hope this will help!

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!

Translated by