How to store in a structure anonymous functions generated by a loop in Matlab? -
i store each anonymous function generated matlab loop in structure (and not in cell) , access , evaluate each stored anonymous function in separated loop. report simple example cell approach. don't know efficient way use structure in context.
gammatrue = 2; deltatrue = -3; t = 4; n = 3; bs = 10; r = 5; bsdensdraws = cell(1, bs); bsdensdrawsev = zeros(t*n*r, bs); w = 1:bs data = randn(t*n, n-1); mutrue = gammatrue/deltatrue*data; sigmatrue = repmat(1/(deltatrue^2)*eye(n-1), [1 1 t*n]); draws = mvnrnd(repmat(mutrue, [r 1]), repmat(sigmatrue, [1 1 r])); %matrix (r*t*n)x(n-1)) bsdensdraws{w} = @(z) mvnpdf(draws,repmat(z(1)/z(2)*data, [r 1]), ... repmat(repmat(1/(z(2)^2)*eye(n-1), [1 1 t*n]), [1 1 r])); end param = [2 3; 4 5; 6 7]; w = 1:bs y = 1:size(param,1) gamma = param(y,1); delta = param(y,2); bsdensdrawsev(:,w) = bsdensdraws{w}([gamma delta]); %vector (t*n*r)x1 end end
you need access fields of struct name, need generate list of field names first. have 10 fields, suggest
fields = char(double('a'):double('a')+bs)'
which generates array containing letters k. preallocate bsdensdraws
bsdensdraws = struct();
and write handles by
bsdensdraws.(fields(w)) = ...
you can access same way. if need more iterations, might need more sophisticated way generate field names.
Comments
Post a Comment