Putting for loop values into a matrix/array for plotting

2 次查看(过去 30 天)
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.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

产品


版本

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by