Azzera filtri
Azzera filtri

2-for loop iteration to output a matrix

3 visualizzazioni (ultimi 30 giorni)
Cutie
Cutie il 14 Feb 2022
Commentato: Cutie il 14 Feb 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]

Risposta accettata

Matt J
Matt J il 14 Feb 2022
Modificato: Matt J il 14 Feb 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 Commenti
Matt J
Matt J il 14 Feb 2022
Modificato: Matt J il 14 Feb 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 il 14 Feb 2022
Thank you @Matt J. The above works perfectly. I appreciate.

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Just for fun in Help Center e File Exchange

Prodotti


Release

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by