2-for loop iteration to output a matrix

4 vues (au cours des 30 derniers jours)
Cutie
Cutie le 14 Fév 2022
Commenté : Cutie le 14 Fév 2022
I want to use for-loop for my entire code for simulation.
Specifically, i want to output results of each iteration with 2 for-loops in a matrix. I use the example code to explain my problem
A=[5 10 15 45] %changing variables in the code
B=[2 4] %another changing variable for iteration
D=[] %final output matrix. I want it to be 2 by 4 (such that output of condition B(1,1) & A(:) makes row1 in D...
for i=1:size(A,2)
w=A(i)
for j=1:size(B,2)
p=B(j)
num=4
mix=2
[ar, A_d, pha, ata] = GmmClas(B, num, mix) %GmmClas
C= gamu(ar, A_d, pha, A) %gamu is a function
D=[D C] %but I want this output in matrix 92 by4)not as vector
end
end
The entire code is working. I only want to create the for-loop for simulation purpose as it will be tediuos manually changing the varying parameters after each iteration.
D is to be the final output (size -2 by 4) where row1 will be output of when B=2 and A=[5 10 15 45] and row 2 will be B=4 and A=[5 10 15 45]

Réponse acceptée

Matt J
Matt J le 14 Fév 2022
Modifié(e) : Matt J le 14 Fév 2022
A=[5 10 15 45]; %changing variables in the code
B=[2 4]; %another changing variable for iteration
D=A+B'
D = 2×4
7 12 17 47 9 14 19 49
  5 commentaires
Matt J
Matt J le 14 Fév 2022
Modifié(e) : Matt J le 14 Fév 2022
A=[5 10 15 45] %changing variables in the code
B=[2 4] %another changing variable for iteration
Na=numel(A); Nb=numel(B);
D=nan(Nb,Na); %final output matrix. I want it to be 2 by 4 (such that output of condition B(1,1) & A(:) makes row1 in D...
for i=1:Na
w=A(i);
for j=1:Nb
p=B(j);
num=4;
mix=2;
[ar, A_d, pha, ata] = GmmClas(B, num, mix); %GmmClas
D(j,i)= gamu(ar, A_d, pha, A); %gamu is a function
end
end
Cutie
Cutie le 14 Fév 2022
Thank you @Matt J. The above works perfectly. I appreciate.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Just for fun dans Help Center et File Exchange

Produits


Version

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by