필터 지우기
필터 지우기

store value after multiple loops

조회 수: 1 (최근 30일)
msh
msh 2016년 11월 10일
댓글: msh 2016년 11월 10일
Hi,
I have four different loops, from which I would like to store the results once an inside loops is finishing. The algorithm works like this:
Let xi, yi, xi, xi, sigmai be vectors with different numbers (values of parameters). What I am doing is I am looping over these parameter values to get some results based on an inner while loop with solving a particular algorithm. I, therefore, construct the following loops:
for n1=1:T
x=xi(n1)
for n2=1:T
y=yi(n2)
for n3=1:T
z=zi(n)
for n4=1:T
simga=sigmai(n4)
mu=z+y+x
while 1>1e-e
results here that depend on the values of sigma and mu
end
end
end
end
end
Can one suggest something practical in order to keep track and store the results in while loop for the different parameter values ?
Thanks

답변 (1개)

Alexandra Harkai
Alexandra Harkai 2016년 11월 10일
Storing results in a multidimensional array:
results = zeros(T, T, T, T);
% your code below
for n1=1:T
x=xi(n1);
for n2=1:T
y=yi(n2);
for n3=1:T
z=zi(n);
for n4=1:T
simga=sigmai(n4);
mu=z+y+x;
while 1>1e-e
results(n1, n2, n3, n4) = % results here that depend on the values of sigma and mu
end
end
end
end
end
  댓글 수: 3
Guillaume
Guillaume 2016년 11월 10일
It's really not clear what the problem is
"I cannot keep track of the different combinations of parameters that this results are produced" why not?
"I was rather trying to see whether I can have [...] a cell array in which I will have one the matrix [...], another matrix [...], another matrices with other things [...]" Sure. So why don't you create that cell array?
Also, do you actually need these 4 nested loops? Assuming that x, y, z are all row vectors. All possible mu can be created with:
mu = x + y(:) + permute(z, [1 3 2]); %in R2016b
mu = bsxfun(@plus, x, bsxfun(@plus, y(:), permute(z, [1 3 2]))); %previous versions
msh
msh 2016년 11월 10일
Guillaume, apologies for the confusion. I now worked out with a combination of your help and other sources what exactly I want to do.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by