CELLFUN syntax when calling the ATAN2 function for 3 x 3 rotation matrix decomposition
Afficher commentaires plus anciens
Hi
I'm trying to use cellfun to apply the following formulas to elements of 3 x 3 rotation matrices, R, stored within a 1 x n cell array, eulR, to derive euler rotations around x, y, z axes (in radians) returned as a 1 x n cell array, eulV:
R = |r11 r12 r13|
|r21 r22 r23|
|r31 r32 r33|
eulx = atan2(r32, r33)
euly = atan2(-r31,sqrt((r32*r32)+(r33*r33))
eulz = atan2(r21, r11)
When applied to a discrete 3 x 3 array I can get the desired output for eulx, euly and eulz using respectively:
eulx = atan2(R(3,2), R(3,3))
euly = atan2(-R(3,1), sqrt(R(3,2)*R(3,2) + R(3,3)*R(3,3)));
eulz = atan2(ans(2,1), ans(1,1));
e.g. for eulR{1}:
0.9437 -0.0012 0.0084
-0.0058 0.5976 -0.0151
0.0037 -0.0166 0.5831
eulx = -0.0284
euly = -0.0063
eulz = -0.0062
However, I am having considerable difficulties in applying the above formulas to each cell of eulR using cellfun. For example, I have tried to use the following code to solve eulx:
eulV = cellfun(@atan2, eulR(3,2), eulR(3,3), 'Uni', 0);
However, this results in the error: 'Index exceeds matrix dimensions'.
Similarily for euly I have tried:
euly = cellfun(@atan2, -eulR(3,1), sqrt(eulR(3,2)*eulR(3,2) + eulR(3,3)*eulR(3,3)), 'Uni', 0);
However, this gives the error message 'Undefined function 'uminus' for input arguments of type 'cell'.
And for eulz I have attempted to use:
eulz = cellfun(@atan2, eulR(2,1), eulR(1,1), 'Uni', 0);
Whilst this does not return an error, in gives an incorrect output in the for of a 3 x 3 matrix stored in a 1 x 1 cell array.
Apologies if this may seem a fairly trivial syntax error, but I have looked at the documentation and cannot see what these errors are relating to (I am still a bit new to coding in Matlab).
Any help would be greatly appreciated.
Thomas
Réponse acceptée
Plus de réponses (1)
Don't use cell arrays to hold the rotation matrices. Use 3x3xN arrays. Then you don't have to deal with cellfun at all. Plus, the code will run faster.
1 commentaire
Thomas Seers
le 25 Déc 2012
Catégories
En savoir plus sur Software Development Tools dans Centre d'aide et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!