Putting for loop values into a matrix/array for plotting

I am trying to find the optimum length of a bridge with regards to cost. I'm trying to put the values from each loop into an array/matrix so I can then find the correspoding road and bridge lengths for the lowest cost on each cost ratio, however the answers from each loop are "seperate" from all other loops and I need them in one array. Does anyone know if this is possible, and if so how I would be able to do this I'm struggling to figure out how it can be done. Thanks.
Ly = 8.7 %total length ravine
Lx = 1.1 %width of ravine
Cs = 1e6 %cost of road per km
for r = 1.1:0.5:5 %varying ratio between road cost per km and bridge cost per km
Ls = 0.1:0.1:8.6; %varying length of road
Lb = sqrt((Ly-Ls).^2 + Lx.^2); %equation to find length of bridge from the length of road
C =((r*Cs)*(Lb))+(Cs*Ls); %equation for total cost of bridge and road
min(C) %minimum value of C for each ratio loop
end

 채택된 답변

Kevin Shi
Kevin Shi 2021년 7월 21일
Hi Daniel,
It seems you are trying to collect the result of each iteration in to one array.
If this is the case, you can initialize a double array before the for loop, then append the result of each iteration to the end of the array, below is an example(I just assume that you want collect minium cost, you can change is to whatever you want later);
Ly = 8.7; %total length ravine
Lx = 1.1; %width of ravine
Cs = 1e6; %cost of road per km
result = [];
for r = 1.1:0.5:5 %varying ratio between road cost per km and bridge cost per km
Ls = 0.1:0.1:8.6; %varying length of road
Lb = sqrt((Ly-Ls).^2 + Lx.^2); %equation to find length of bridge from the length of road
C =((r*Cs)*(Lb))+(Cs*Ls); %equation for total cost of bridge and road
result(end + 1) = min(C) %minimum value of C for each ratio loop
end
Hope this is helpful.

댓글 수: 1

That did exactly what I wanted thank you so much for the quality and the speed of the reply.

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품

릴리스

R2021a

질문:

2021년 7월 20일

댓글:

2021년 7월 23일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by