# Iteratively concatenate matrices held in a structure with column vectors stored in an 3xn matrix

2 views (last 30 days)
Thomas on 10 Oct 2012
I have a series of 3x3 rotation matrices calculated from a 3xn matrix where columns are euler rotations. These are stored in a structure (euler) provided by:
%Calculates rotation matrices for each camera position, stores them
%inside a structure (euler) and create dynamics fieldnames
%(euler.PV1,..,PVn)
euler=struct();
for k=1:size(MPV,2)
Rxk = [1 0 0; 0 cosd(MPV(1,k)) -sind(MPV(1,k)); 0 sind(MPV(1,k)) cosd(MPV(1,k))];
Ryk = [cosd(MPV(2,k)) 0 sind(MPV(2,k)); 0 1 0; -sind(MPV(2,k)) 0 cosd(MPV(2,k))];
Rzk = [cosd(MPV(3,k)) -sind(MPV(3,k)) 0; sind(MPV(3,k)) cosd(MPV(3,k)) 0; 0 0 1];
RMk = Rzk*Ryk*Rxk;
euler.(['PV' num2str(k)])=RMk;
end
The resultant structure is:
PV1: [3x3 double]
PV2: [3x3 double]
PV3: [3x3 double]
PV4: [3x3 double]
PV5: [3x3 double]
PV6: [3x3 double]
PV7: [3x3 double]
Where individual matrices have the form: >> euler.PV1
ans =
0.9994 -0.0251 0.0256
0.0254 0.9996 -0.0082
-0.0254 0.0088 0.9996
I also have a 3xn matrix (MPV where columns contain x, y, z components of column vectors:
MPV =
-0.9741 -1.7308 -1.6931 -1.7115 ...
0.9437 -0.0551 -0.0210 -0.0079 ...
0.9437 -0.0551 -0.0210 -0.0079 ...
My problem is that I want to iteratively concatenate the 3x3 matrices stored in the euler structure with the x, y, z components stored in MPV to give a matrix of the form:
r11 r12 r13 x
r21 r22 r23 y
r31 r32 r33 z
I thought that a for loop of the form:
for j=1:size(euler.PV)
MM = vertcat(PV(j),MPV(i));
Mat.(['M' num2str(j)])=MMj;
end
might do the trick, but gives the error:
Undefined function or variable 'M'.
Error in matcon (line 42)
for j=1:length(M)
Note that I need to perform a further horizontal concatenation for each matrix and a single row array of the form ID = [0 0 0 1](i.e. horzcat(mat.M1,..Mn,ID) to create 4 x 4 matrices (homogenous coordinates), so having an output as a structure may be desirable at this stage. Apologies in advance for this noob-like question. Any help with the code would be much appreciated.

Andrei Bobrov on 11 Oct 2012
Edited: Andrei Bobrov on 11 Oct 2012
outcl = cellfun(@(x,y)[x,y],struct2cell(euler),num2cell(MPV,1)','un',0);
eulernew = cell2struct(outcl,fields(euler),1);
##### 1 CommentShowHide None
Thomas on 11 Oct 2012
Wow! fantastic. Code works beautifully. It may take me a while to work out what's going on here. Thanks again Andrei. Thomas

### Community Treasure Hunt

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

Start Hunting!