Help with nested for loops

2 ビュー (過去 30 日間)
Emma
Emma 2025 年 6 月 11 日
コメント済み: Torsten 2025 年 6 月 12 日
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
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
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
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
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 ExchangeStress and Strain についてさらに検索

タグ

製品


リリース

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by