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
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
채택된 답변
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
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!