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

2 次查看(过去 30 天)
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 个评论
Thomas Seers
Thomas Seers 2012-10-11
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

请先登录,再进行评论。

采纳的回答

Matt J
Matt J 2012-10-11
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 个评论
Matt J
Matt J 2012-10-11
Make that
newstruct=structfun(@(c) [c;[0 0 0 1]], eulernew, 'uni',0);
Also, see "doc structfun" so that you understand what's going on!
Thomas Seers
Thomas Seers 2012-10-11
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

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by