How to store all outputs from this nested for loop
2 views (last 30 days)
Show older comments
I have this for loop which goes through 3 different variables. I need to record all outputs so that I can plot the 3 variables and the outputs as a 3D colour surface plot similar to the second plot here https://www.mathworks.com/help/matlab/ref/surf.html
At the moment it only gives the last output so if someone can point me in the right direction to gettiing all outputs.
for s=0:9 % Variable 1
for om=0.1:0.1:1 % Variable 2
for mu=0.1:0.1:1 % Variable 3
nll = mylikelihood(s,om,mu); % This is a function I made.
end
end
end
Also if anyone could see how I can vectorise the code so that it runs faster that would be appreciated.
2 Comments
Walter Roberson
on 9 Jul 2021
That code cannot be vectorized unless your function can be vectorized, but you did not show the code for your function so we do not know what can be done with it.
Accepted Answer
Stephen23
on 9 Jul 2021
Vs = 0:9; % Variable 1
Vom = 0.1:0.1:1; % Variable 2
Vmu = 0.1:0.1:1; % Variable 3
Ns = numel(Vs);
Nom = numel(Vom);
Nmu = numel(Vmu);
nll = nan(Ns,Nom,Nmu);
for k1 = 1:Ns
for k2 = 1:Nom
for k3 = 1:Nmu
nll(k1,k2,k3) = mylikelihood(Vs(k1),Vom(k2),Vmu(k3));
end
end
end
More Answers (0)
See Also
Categories
Find more on Graphics Performance in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!