Help with nested for loops
2 次查看(过去 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!