arrays - sorting 3D matrix by using 2D matrix -
arrays - sorting 3D matrix by using 2D matrix -
hello trying sort 3d coordinates matrix (3 columns , unlimited rows) using sorting in 2d coordinates matrix (2 columns , unlimited rows):
a=[x1 y1 z1;x2 y2 z2;xn yn zn]; b=[xn yn;x1 y1;x2 y2];
example:
a=[87 45 47;87 66 47;80 40 48;81 41 48;87 45 48;87 66 48;69 39 49;73 39 49;79 40 49;81 71 49;86 67 49;70 39 50;74 38 50;79 40 50;82 70 50;85 68 50;71 39 51;75 38 51];
b=[87 45;87 45;81 41;80 40;79 40;79 40;75 38;74 38;73 39;71 39;70 39;69 39;87 66;87 66;86 67;85 68;82 70;81 71];
i sort based on b, means have in end as:
anew=[xn yn zn;x1 y1 z1;x2 y2 z2]
;
this illustration of data. matrix of xyz coordinate , b xy only.
is way of doing or maybe can seek help me out ?
edit: used method proposed jan , working. give thanks , sorry if didn't give plenty info in initial post.
if understand correctly, want reorder rows of such first 2 columns of matrix equal (hence match order of) matrix b. first of all, rows in b not unique, solution not unique.
anyway, 1 approach involve sorting both rows obtain desired new ordering. approach cumbersome, works this:
first, obtain ordering after sorting action on a(:,1:2)
, b
:
[~, ordera] = sortrows(a(:,1:2)); [~, orderb] = sortrows(b);
then, order of action 'inverses' sec sorting:
[~,orderbinv] = sort(orderb);
now, ordering of a(:,1:2)
equal ordering of b
, combine inverse sorting , sorting this:
anew = a(ordera(orderbinv),:)
ok, have done here is: take inverse sorting sorted version of b
result in original. sorted b
matches sorted a(:,1:2)
(in case of unique rows), applying order ordering results sorting a
gives desired result.
so, given a'
(transposed reading convenience)
87 87 80 81 87 87 69 73 79 81 86 70 74 79 82 85 71 75 45 66 40 41 45 66 39 39 40 71 67 39 38 40 70 68 39 38 47 47 48 48 48 48 49 49 49 49 49 50 50 50 50 50 51 51
and b'
87 87 81 80 79 79 75 74 73 71 70 69 87 87 86 85 82 81 45 45 41 40 40 40 38 38 39 39 39 39 66 66 67 68 70 71
the resulting anew'
equals
87 87 81 80 79 79 75 74 73 71 70 69 87 87 86 85 82 81 45 45 41 40 40 40 38 38 39 39 39 39 66 66 67 68 70 71 47 48 48 48 49 50 51 50 49 51 50 49 47 48 49 50 50 49
arrays matlab matrix vector 3d
Comments
Post a Comment