2-for loop iteration to output a matrix

3 次查看(过去 30 天)
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]

采纳的回答

Matt J
Matt J 2022-2-14
编辑:Matt J 2022-2-14
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 个评论
Matt J
Matt J 2022-2-14
编辑:Matt J 2022-2-14
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 2022-2-14
Thank you @Matt J. The above works perfectly. I appreciate.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Just for fun 的更多信息

产品


版本

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by