Combining vectors in a for loop to form a final matrix

4 次查看(过去 30 天)
Hi there,
I am writing a script to solve problems using the Moment Distribution Method for structural analysis. I have 4 loops that generate row vectors. What I want to do is combine the final calculated vectors into one large matrix. Here is the code:
DF = [0 0.625 0.375 0.6 0.4 0];
FEM = [-10 10 -18 12 -16 16];
MBal = [FEM(2)+FEM(3) FEM(4)+FEM(5)];
if MBal < 0
MBal = abs(MBal);
end
Fdistribution = [0 DF(2:3)*MBal(1) DF(4:5)*MBal(2) 0];
for i = 1:4
Fdistribution([1,2,3,4,5,6]) = Fdistribution([2,1,4,3,6,5]);
Carry_over = Fdistribution./2;
idistribution = [ 0 Carry_over(3).*DF(2:3) Carry_over(4).*DF(4:5) 0];
if idistribution >= 0
idistribution = -idistribution;
else idistribution = abs(idistribution);
end
Fdistribution = idistribution;
if i == 4
idistribution(1) = idistribution(2)/2;
idistribution(6) = idistribution(5)/2;
end
Table = [Carry_over; idistribution]
end
Table = 2×6
2.5000 0 1.2000 1.5000 0 0.8000 0 -0.7500 -0.4500 -0.9000 -0.6000 0
Table = 2×6
-0.3750 0 -0.4500 -0.2250 0 -0.3000 0 0.2812 0.1687 0.1350 0.0900 0
Table = 2×6
0.1406 0 0.0675 0.0844 0 0.0450 0 -0.0422 -0.0253 -0.0506 -0.0337 0
Table = 2×6
-0.0211 0 -0.0253 -0.0127 0 -0.0169 0.0079 0.0158 0.0095 0.0076 0.0051 0.0025
Basically I want to combine the vector 'Table' so its displays all the Table vectors together. So, I would have a 8 by 6 matrix.
Can anybody help please? Then I can manipulate the matrix and sum up the columns.
Many thanks for any help.

采纳的回答

Mathieu NOE
Mathieu NOE 2023-7-20
For small tables / arrays I don't pay too much attention to preallocation (but it's important if you are dealing with large data !!)
so the basic approach here is simply to concatenate your results
DF = [0 0.625 0.375 0.6 0.4 0];
FEM = [-10 10 -18 12 -16 16];
MBal = [FEM(2)+FEM(3) FEM(4)+FEM(5)];
tmp = [];
Table = [];
if MBal < 0
MBal = abs(MBal);
end
Fdistribution = [0 DF(2:3)*MBal(1) DF(4:5)*MBal(2) 0];
for i = 1:4
Fdistribution([1,2,3,4,5,6]) = Fdistribution([2,1,4,3,6,5]);
Carry_over = Fdistribution./2;
idistribution = [ 0 Carry_over(3).*DF(2:3) Carry_over(4).*DF(4:5) 0];
if idistribution >= 0
idistribution = -idistribution;
else idistribution = abs(idistribution);
end
Fdistribution = idistribution;
if i == 4
idistribution(1) = idistribution(2)/2;
idistribution(6) = idistribution(5)/2;
end
tmp = [Carry_over; idistribution];
Table = [Table; tmp];
end
Table
Table = 8×6
2.5000 0 1.2000 1.5000 0 0.8000 0 -0.7500 -0.4500 -0.9000 -0.6000 0 -0.3750 0 -0.4500 -0.2250 0 -0.3000 0 0.2812 0.1687 0.1350 0.0900 0 0.1406 0 0.0675 0.0844 0 0.0450 0 -0.0422 -0.0253 -0.0506 -0.0337 0 -0.0211 0 -0.0253 -0.0127 0 -0.0169 0.0079 0.0158 0.0095 0.0076 0.0051 0.0025

更多回答(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