Combining vectors in a for loop to form a final matrix
4 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Scott Banks
le 20 Juil 2023
Commenté : Mathieu NOE
le 20 Juil 2023
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
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.
0 commentaires
Réponse acceptée
Mathieu NOE
le 20 Juil 2023
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
2 commentaires
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Creating and Concatenating Matrices dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!