How to sort coordinates into a multi dimensional array?
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
Hi,
I have three points P1(x1,y1), P2(x2,y2) and P3(x3,y3) and there are 42 values of these points. Through calculation I have individual arrays of x1, x2, y1, y2, x3 and y3. How do I sort them into a multi dimensional array. I want it to be in the form of P = [(x1,y1),(x2,y2),(x3,y3)] and 42 elements in the other dimensions making it a 4D array i.e. 1 Row: 3 Columns: 2 Elements in each column: 42 Layers.
2 comentarios
KALYAN ACHARJYA
el 30 de Sept. de 2019
Can you elaborate more easily (with examples)? Please note that numerical or math is much easier to understand than long text.
Respuestas (1)
Raunak Gupta
el 4 de Oct. de 2019
Hi,
For creating a 4D array you may use array indexing however for using sort, the dimension on which sorting is needed may be clearly visualized as the structure of array changes after choosing a particular dimension. For creating 4D array so may follow the below example.
X1 = randi(12,1,42); % These are individual row vectors
Y1 = randi(12,1,42);
X2 = randi(12,1,42);
Y2 = randi(12,1,42);
X3 = randi(12,1,42);
Y3 = randi(12,1,42);
Coordinates(1,:,:) = [X1;Y1]; %P1
Coordinates(2,:,:) = [X2;Y2]; %P2
Coordinates(3,:,:) = [X3;Y3]; %P3
final(1,:,:,:) = Coordinates;
% Above final Matrices will be of size 1 x 3 x 2 x 42
If it is required to sort along 2nd dimension with shows P1,P2,P3 then the sort function will compare the values P1,P2,P3 for all corresponding 2 X 42 matrix and will arrange them as required in ascending and descending order. So, the pairing of previous (x11,y11) , (x12,y12) etc. will change. That is why it is recommended to visualize how the sorting is required or what should be the end result.
0 comentarios
Ver también
Categorías
Más información sobre Shifting and Sorting Matrices en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!