Iteratively concatenate 3x4 matrices held in a structure with single line arrays held in a structure

11 views (last 30 days)
Thomas
Thomas on 11 Oct 2012
I have a structure, eulernew holding n 3x4 matrices (eulernew.RMn). eulernew =
RM1: [3x4 double]
RM2: [3x4 double]
RM3: [3x4 double]
RM4: [3x4 double]
RM5: [3x4 double]
RM6: [3x4 double]
Where eulernew.RM1 =
0.9994 -0.0251 0.0256 0.5053
0.0254 0.9996 -0.0082 1.4532
-0.0254 0.0088 0.9996 1.4532
I have written the following code to create a structure, IDA:
%Create origin / vector multiple array for the bottom rows of the 4 x 4
%matrices
ID = [0 0 0 1];
%Create a structure,IDA where n structure elements (IDA.I=ID)
%equate to the size, PX
IDA=struct();
for i=1:size(PX)
IDA.(['I' num2str(i)])=ID;
end
Which contains n single row arrays, I1,..,In, corresponding to the number of 3x4 arrays held in eulernew:
I1: [0 0 0 1]
I2: [0 0 0 1]
I3: [0 0 0 1]
I4: [0 0 0 1]
I5: [0 0 0 1]
I6: [0 0 0 1]
My problem is that a want to sequentially concatenate eulernew.RM1,..,RMn with IDA.I1,..,In to produce a structure containing arrays of the form:
r11 r12 r13 x
r21 r22 r23 y
r31 r32 r33 z
0 0 0 1
On the command line, I can perform this calculation on an individual basis using the command:
vertcat(eulernew.RM1,IDA.I1);
I thought about using a struct2cell function such as:
c1 = struct2cell(eulernew);
c2 = struct2cell(IDA);
Then performing the concatenation, but ended up tying myself in knots! The syntax for performing concatenation on arrays contained in two separate structures is beyond me at present (I’m very new to the programming aspect of Matlab).
Any help would be greatly appreciated.
Thomas
  1 Comment
Thomas
Thomas on 11 Oct 2012
I thought this for loop might do it:
R = struct2cell(eulernew);
I = struct2cell(IDA);
Mat=struct();
for i=1:length(PX);
Mat.(['M' num2str(i)])=[R,I];
end
But this seems to concatenate the contents of each struct (eulernew,IDA) populates the fields Mat.M1,..,Mn with every array in eulernew and IDA. For example:
Mat.M1
ans =
[3x4 double] [1x4 double]
[3x4 double] [1x4 double]
[3x4 double] [1x4 double]
[3x4 double] [1x4 double]
[3x4 double] [1x4 double]
[3x4 double] [1x4 double]
[3x4 double] [1x4 double]
[3x4 double] [1x4 double]
[3x4 double] [1x4 double]
[3x4 double] [1x4 double]
[3x4 double] [1x4 double]
[3x4 double] [1x4 double]
[3x4 double] [1x4 double]
[3x4 double] [1x4 double]
[3x4 double] [1x4 double]
[3x4 double] [1x4 double]
[3x4 double] [1x4 double]
[3x4 double] [1x4 double]
[3x4 double] [1x4 double]
[3x4 double] [1x4 double]
[3x4 double] [1x4 double]
[3x4 double] [1x4 double]
[3x4 double] [1x4 double]
[3x4 double] [1x4 double]
[3x4 double] [1x4 double]
[3x4 double] [1x4 double]
[3x4 double] [1x4 double]
[3x4 double] [1x4 double]
[3x4 double] [1x4 double]
[3x4 double] [1x4 double]
Close(ish), or maybe not? Could anyone modify this form to give the desired result?
Thomas

Sign in to comment.

Accepted Answer

Matt J
Matt J on 11 Oct 2012
This seems like a very unwieldy way to organize your data. Why hold things in structs? Why not hold all your RM matrices in the slices of a single 3D array
RM(:,:,i)=RMi
That would make both indexing and concatenating them much easier. Regardless, you can do this
newstruct=structfun(@(c) [c;[0 0 0 1]], RM, 'uni',0);
  3 Comments
Thomas
Thomas on 11 Oct 2012
Cheers Matt. Genius. Please ignore my dopey comment above. The web page hadn't been refreshed, so I didn't see this. Will read the doc and work through it.
Thanks again.
Thomas

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!