필터 지우기
필터 지우기

Combining vectors in a for loop to form a final matrix

조회 수: 5 (최근 30일)
Scott Banks
Scott Banks 2023년 7월 20일
댓글: Mathieu NOE 2023년 7월 20일
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
  댓글 수: 2
Scott Banks
Scott Banks 2023년 7월 20일
Thank you very much for your help, Mathieu!
Mathieu NOE
Mathieu NOE 2023년 7월 20일
my pleasure !!

댓글을 달려면 로그인하십시오.

추가 답변 (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