CELLFUN syntax when calling the ATAN2 function for 3 x 3 rotation matrix decomposition

13 views (last 30 days)
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.

Accepted Answer

Azzi Abdelmalek
Azzi Abdelmalek on 25 Dec 2012
Edited: Azzi Abdelmalek on 25 Dec 2012
euly=cellfun(@(x) atan2(-x(3,1),sqrt(x(3,2)*x(3,2)+x(3,3)*x(3,3))),eulR);
  1 Comment
Thomas on 25 Dec 2012
Thank you very much Azzi. I did play around with @(x) but must have misplaced the brackets somewhere along the line.
The other euler angles can be solved using:
eulx = cellfun(@(x) atan2(x(3,2), (x(3,3))), eulR);
eulz = cellfun(@(x) atan2(x(2,1), (x(1,1))), eulR);
Incase anyone was interested.
Thanks again

Sign in to comment.

More Answers (1)

Matt J
Matt J on 25 Dec 2012
Edited: Matt J on 25 Dec 2012
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 Comment
Thomas on 25 Dec 2012
Thanks for your advice Matt.
I will certainly look at the option of using 3x3xN arrays. Basically, at the moment, I am just trying to get the code to work whilst learning along the way. Cell arrays where one of the first solutions I came across so I've kind of stuck with them for better or worse (probably worse!). I'm endeavouring to improve (it's certainly fun trying anyway).
Thanks again.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!