I want to store the output from my nested for loop in a matrix . How can it be done?
조회 수: 2 (최근 30일)
이전 댓글 표시
Bw=[150 155];
Rbwtobtf=[0.2,0.21];
Hf=[70 75];
Rdtoh=[0.85 0.86];
Tbf=[115 120];
Bbf= [250 260];
Mu=3400000000;
Fcu=50;
Rdtoh=[0.85 0.86];
Tbf=[115 120];
Bbf=[250 260];
for i=1:length(Bw)
for j=1:length(Rbwtobtf)
for k=1:length(Hf)
for l= 1:length(Rdtoh)
for m= 1:length(Tbf)
for n= 1: length(Bbf)
Btf=Bw(i)/Rbwtobtf(j);
syms x;
d=solve(Mu==Fcu*Btf*x^2*(0.157*Rbwtobtf(j)+(1-Rbwtobtf(j))*(Hf(k)/x)*(0.45-0.225*(Hf(k)/x))),x);
D=vpa(d);
De=D(1);
h= De/Rdtoh(l);
A1 = Btf*Hf(k);% Area of top flange
Hw = h-Hf(k)-Tbf(m);
A2 = Bw(i)*Hw; %Area of web
A3 = Bbf(n)*Tbf(m); %Area of bottom flange
A = A1+A2+A3; %Total area
disp (A);
n=n+1;
end
m=m+1;
end
l=l+1;
end
k=k+1;
end
j=j+1;
end
i=i+1;
end
I want to store values of A in a matrix. please help.
댓글 수: 1
Stephen23
2017년 5월 26일
It seems like most/all of this could be vectorized quite easily:
That would be simpler and faster too.
답변 (2개)
MathReallyWorks
2017년 5월 26일
Hello Vinay,
Firstly, Make the code readable. It will be easy for us to solve your problem.
Anyways, your problem seems to be of multi dimensional array.
Use
A(n,m,l,k,j,i) = A1+A2+A3; %Total area
or
A(i,j,k,l,m,n) = A1+A2+A3; %Total area
in your code for storing the data in A.
Suggestion: Remove disp(A) which is just below this line.
Andrei Bobrov
2017년 5월 26일
편집: Andrei Bobrov
2017년 5월 26일
[EDIT]
Bw=[150 155];
Rbwtobtf=[0.2,0.21];
Hf=[70 75];
Rdtoh=[0.85 0.86];
Tbf=[115 120];
Bbf= [250 260];
Mu=3400000000;
Fcu=50;
[a,b,c0] = ndgrid(Hf,Rbwtobtf,Bw);
c = [c0(:),b(:),a(:)];
r0 = Fcu*c(:,1);
r = 9*r0.*c(:,3).*( 1./c(:,2) - 1);
p = [(157*r0)/1000, r/20, - Mu - r.*c(:,3)/40];
s = size(p,1);
dd = zeros(s,1);
for ii = 1:s
q = roots(p(ii,:));
dd(ii) = q(1);
end
[a,b,c0,n] = ndgrid(Bbf,Tbf,Rdtoh,1:s);
c = [c(n(:),:),c0(:),b(:),a(:)];
AA = c(:,1).*(c(:,3)./c(:,2) + dd(n(:))./c(:,4) - c(:,3) - c(:,5)) + c(:,6).*c(:,5);
댓글 수: 3
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!