How to sort coordinates into a multi dimensional array?
8 次查看(过去 30 天)
显示 更早的评论
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 个评论
KALYAN ACHARJYA
2019-9-30
Can you elaborate more easily (with examples)? Please note that numerical or math is much easier to understand than long text.
回答(1 个)
Raunak Gupta
2019-10-4
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 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Shifting and Sorting Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!