Help with nested for loops

8 vues (au cours des 30 derniers jours)
Emma
Emma le 11 Juin 2025
Commenté : Torsten le 12 Juin 2025
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 commentaire
Torsten
Torsten le 11 Juin 2025
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.

Connectez-vous pour commenter.

Réponses (1)

Walter Roberson
Walter Roberson le 11 Juin 2025
Déplacé(e) : Matt J le 12 Juin 2025
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 commentaires
Walter Roberson
Walter Roberson le 12 Juin 2025
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 le 12 Juin 2025
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).

Connectez-vous pour commenter.

Catégories

En savoir plus sur Stress and Strain dans Help Center et File Exchange

Tags

Produits


Version

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by