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