How do you store solutions to a loop in an array?

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.

답변 (2개)

Rik
Rik 2017년 12월 14일
편집: Rik 2017년 12월 14일

0 개 추천

%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

How do I add findND?
Rik
Rik 2017년 12월 14일
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
Star Strider 2017년 12월 14일

0 개 추천

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

j is meant to be h, I messed up the code. I am trying to find all of the possible solutions to the loop (where v>= volume and hoop max is <= allow_stress) with three values (radius,thickness,height), (r,t,h).
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.
I am getting this error "Assignment has more non-singleton rhs dimensions than non-singleton subscripts" After the
x(ri, ti, hi) = [r(ri),t(ti),h(hi)];
Rik
Rik 2017년 12월 14일
If gamma_molasses is indeed a scalar, why not avoid the loops and use my solution?
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.
This still gives me the same error.
Rik
Rik 2017년 12월 14일
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.

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

카테고리

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

태그

질문:

2017년 12월 14일

댓글:

Rik
2017년 12월 14일

Community Treasure Hunt

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

Start Hunting!

Translated by