Help with nested for loops
조회 수: 3 (최근 30일)
이전 댓글 표시
I'm trying to run a loop to solve an equation with multiple variables and I think I should get more than 9 answers.
Two questions:
First, are my nested loops acurrate/is the syntax right?
Second, how would I determine how many possiblities there are for stress? (See my pre-allocated zeros)
For reference I added the values next to each of the variables in parenthesis.
I really appreciate any help!
%Statistical parameter for post_tullis piezometer
gmean_low = 10^(mean(log10(drex))); % Geometric mean for Anorthite
a1std_low = std(drex); % 1 standard deviation for Anorthite
%plug it into the piezometer (Anorthite)
%%Variables for the piezometer
D1_0=gmean_low; %Middle (5.0736)
D1_1=gmean_low - a1std_low; %Lower bound (3.0434)
D1_2=gmean_low + a1std_low; %Upper bound (7.1038)
K1_0=55; %Middle (55)
K1_1=55-5; %Lower bound (50)
K1_2=55+5; %Upper bound (60)
Q1_0=-0.66; %Middle (-0.6600)
Q1_1=-0.66-0.07; %Lower bound (-0.7300)
Q1_2=-0.66+0.07; %Upper bound (-0.5900)
%%Stress Calculations
Duse=[D1_0, D1_1, D1_2]; %Array for D-values
Kuse=[K1_0, K1_1, K1_2];
Quse=[Q1_0, Q1_1, Q1_2];
Stress=zeros(5832,1);
for i=1:length(Duse)
Dexplore=Duse(i);
for k=1:length(Kuse)
Kexplore=Kuse(k);
for j=1:length(Quse)
Qexplore=Quse(j);
Stress(i, k, j)=(Dexplore/Kexplore)^(1/Qexplore);
end
end
end
댓글 수: 1
Torsten
2025년 6월 11일
The way you save "Stress", it should be a matrix of size (3x3x3), thus
Stress = zeros(numel(Duse),numel(Kuse),numel(Quse))
should work fine.
답변 (1개)
Walter Roberson
2025년 6월 11일
이동: Matt J
2025년 6월 12일
Vectorized version with no for loops:
Duse=[D1_0, D1_1, D1_2]; %Array for D-values
Kuse=[K1_0, K1_1, K1_2];
Quse=[Q1_0, Q1_1, Q1_2];
Stress = (reshape(Duse,[],1) ./ reshape(Kuse, 1, [])) .^ (1./reshape(Quse,1,1,[]));
댓글 수: 5
Walter Roberson
2025년 6월 12일
The variables are named K1_0, K1_1, K1_2 . Index I runs from 1 to 3, so if you used I as the subscript you would be referring to K1_1, K1_2, K1_3 . You have to subtract 1 from the subscript to get the variable name.
Torsten
2025년 6월 12일
Ah, I see now. I was thinking in element I of Duse, element J of Kuse and element K of Quse to compute Stress(I,J,K).
참고 항목
카테고리
Help Center 및 File Exchange에서 Stress and Strain에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!