필터 지우기
필터 지우기

2-for loop iteration to output a matrix

조회 수: 3 (최근 30일)
Cutie
Cutie 2022년 2월 14일
댓글: Cutie 2022년 2월 14일
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