How do I convert a group of 3x3 matrices that are contained in a 3xN array, into a 3x3xN/3 array?

8 vues (au cours des 30 derniers jours)
I'm a bit at a loss. I have an array of 2 different 3x3 rotation matrices represented in a 3x6 array, I'd like to conver that to a 3x3x2 array, but it converts in a way that doesn't match the original order of the 3x3 arrays.
I'm looking to start here
A =
[0.9053 -0.4246 -0.0002;
0.4246 0.9053 0.0001;
0.0002 -0.0002 0.9999;
0.9053 -0.4245 0.0001;
0.4245 0.9053 0.0000;
-0.0001 0.0000 0.9999];
and end here
B =
val(:,:,1) =
0.9053 -0.4246 -0.0002;
0.4246 0.9053 0.0001;
0.0002 -0.0002 0.9999
val(:,:,2) =
0.9053 -0.4245 0.0001;
0.4245 0.9053 0.0000;
-0.0001 0.0000 0.9999
I can't represent it in the tags, but I have all official matlab toolboxes

Réponse acceptée

Cris LaPierre
Cris LaPierre le 18 Oct 2021
MATLAB uses column-major order by default, which means the data is organized internally as if the columns were stacked head-to-tail on tope of each other (what you see when you do A(:)). When you reorgainze the data to be 3x3xN, it fills the columns of the new array column by column, using all the data in the first column of A, then the second, etc.
Since your columns stay fixed, you can take advantage of the transpose operator along with the reshape and permute functions.
A = [0.9053 -0.4246 -0.0002;
0.4246 0.9053 0.0001;
0.0002 -0.0002 0.9999;
0.9053 -0.4245 0.0001;
0.4245 0.9053 0.0000;
-0.0001 0.0000 0.9999];
% Reshape and permute
val = reshape(A',3,3,[]);
val = permute(val,[2,1,3])
val =
val(:,:,1) = 0.9053 -0.4246 -0.0002 0.4246 0.9053 0.0001 0.0002 -0.0002 0.9999 val(:,:,2) = 0.9053 -0.4245 0.0001 0.4245 0.9053 0 -0.0001 0 0.9999

Plus de réponses (0)

Catégories

En savoir plus sur Data Type Conversion dans Help Center et File Exchange

Produits


Version

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by