store value after multiple loops
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
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
0 Kommentare
Antworten (1)
Alexandra Harkai
am 10 Nov. 2016
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 Kommentare
Guillaume
am 10 Nov. 2016
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
Siehe auch
Kategorien
Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!