How do you store solutions to a loop in an array?
Ältere Kommentare anzeigen
for r = 10:10:150
for t =.1:.1:2
for h = 45:.1:50
v = pi*(r^2)*h
hoop_max = (gamma_molasses*(h*12)*(r*12))/(t)
if v >= volume & hoop_max <= allow_stress;
x= [r,t,j]
else
end
end
end
end
This is a basic question, but I can't figure out how to store the solutions to this in the array x, such that each solution takes up one row.
Antworten (2)
%replace
x=[r,t,j];
%with
k=k+1;x(k,:)=[r,t,j];
Set k to 0 before the loops and pre-allocate at least one row of x, but preferably as close to the number of solutions as you can.
A much better idea would be to use meshgrid to generate a 3D matrix for each loop value, which will enable you to calculate all of this at once and remove all loops. If you get findND, you can use the code below.
%just some random numbers:
gamma_molasses=1;volume=50000;allow_stress=10^5;
[r,t,h]=meshgrid(10:10:150,.1:.1:2,45:.1:50);
v=pi*(r.^2).*h;
hoop_max = (gamma_molasses.*(h*12).*(r*12))./(t);
[r2,t2,h2]=findND(v >= volume & hoop_max <= allow_stress);
x=[r2,t2,h2];
2 Kommentare
Ivan Arkhipov
am 14 Dez. 2017
Rik
am 14 Dez. 2017
You can download it from the File Exchange with the link in my post ( here it is again). To use it, just make sure the m-file is in the current folder, or in another folder on the Matlab path.
Star Strider
am 14 Dez. 2017
In MATLAB, subscripts can only be integers greater than 0, or logical variables.
I do not follow what you are doing. This will at least provide the correct indices:
r = 10:10:150;
t =.1:.1:2;
h = 45:.1:50;
for ri = 1:length(r)
for ti = 1:length(t)
for hi = 1:length(h)
v = pi*(r(ri)^2)*h(hi);
hoop_max = (gamma_molasses*(h(hi)*12)*(r(ri)*12))/(t(ti));
if v >= volume & hoop_max <= allow_stress;
x(ri, ti, j) = [r(ri),t(ti),j]
else
end
end
end
end
NOTE — Because I do not know what you are calculating, this is UNTESTED CODE. The indexing should work.
Also, you did not tell us what ‘j’ is.
7 Kommentare
Ivan Arkhipov
am 14 Dez. 2017
Star Strider
am 14 Dez. 2017
In that event:
x(ri, ti, hi) = [r(ri),t(ti),h(hi)];
and:
hoop_max(ri, ti, hi) = (gamma_molasses*(h(hi)*12)*(r(ri)*12))/(t(ti));
I still do not follow what you are doing. The important aspect appears to be to get the array referencing correct.
Rearrange the indices in the order you want them to appear in your matrices.
Ivan Arkhipov
am 14 Dez. 2017
Rik
am 14 Dez. 2017
If gamma_molasses is indeed a scalar, why not avoid the loops and use my solution?
Star Strider
am 14 Dez. 2017
Add a dimension:
x(ri, ti, hi, :) = [r(ri),t(ti),h(hi)];
That should eliminate the error, at the expense of creating a matrix with an additional dimension.
Ivan Arkhipov
am 14 Dez. 2017
Rik
am 14 Dez. 2017
For this syntax to work, you will need to pre-allocate x. (this should do the trick: x = zeros(1,1,1,3);)
Also note that this will give you a different result from my solution.
Kategorien
Mehr zu Loops and Conditional Statements finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!