Storing a Matrix Output in a matrix
3 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Gbeminiyi Oyedele
il 18 Lug 2022
Commentato: Gbeminiyi Oyedele
il 18 Lug 2022
%% I am running the following code but having some error:
prevalence=zeros(iters,1+max(data.t));
for i=1:iters
% copy existing parameter vector
proposal=theta;
% Generate a new proposal for epsilon
proposal=normrnd(theta(1),sigma);
% check proposal is in range
if proposal(1)>0
% calculate log acceptance ratio (may be bigger than 1)
lar = logLikelihoodSIR2(proposal,data,N) - logLikelihoodSIR2(theta(1),data,N);
% don't forget the prior ratio!
lar = lar + logPrior(proposal) - logPrior(theta(1));
% generate a random number between 0 and 1;
u = unifrnd(0,1);
% accept if lar>log(u) iff ap>u
if lar>log(u)
% the proposal becomes the new value of theta
theta=proposal;
accept=accept+1;
else
reject=reject+1;
end
else
% automatically reject outside the range for beta (as it has prior
% density zero).
reject=reject+1;
end
% store parameters for output every iteration (at the moment)
stored(i,:)=theta;
% recalculate and store trajectory for output
Numgroups = 5;
N = [600000 800000 1000000 1500000 6000000];
sigma = [0.01 0.05 0.2 0.3 0.4];
para = struct('N',N,'H',0.25,'da',0.3,'tau',0.15,'gamma',0.1, ...
'epislon',theta(1),'Numgroups',Numgroups,'sigma',sigma,'q_s',0.05, ...
'q_w',0.2, 'q_o',0.5,'q_h',1/4,'phi_t',0.25,'theta',0.45);
S = [600000 800000 1000000 1500000 6000000] - [1 zeros(1,Numgroups - 1)];
%Define initial conditions as a structure
ICs = struct('S',S,'E_f',[1 zeros(1,Numgroups - 1)],'E_sd',zeros(1,Numgroups), ...
'E_su',zeros(1,Numgroups),'E_q',zeros(1,Numgroups),'D_f',zeros(1,Numgroups), ...
'D_sd',zeros(1,Numgroups),'D_su',zeros(1,Numgroups),'D_qf',zeros(1,Numgroups), ...
'D_qs',zeros(1,Numgroups),'U_f',zeros(1,Numgroups),'U_s',zeros(1,Numgroups), ...
'U_q',zeros(1,Numgroups),'N',N);
Classes=ODE_agemodelkeelMCMC(para,ICs,min(data.t),max(data.t));
prevalence(i,:) = Classes.E_f./N;
end
My question is in the BOLDEN PART.
Classes.E_f./N is an (844 by 5) matrices which is what I want.
Prevalence(i,:) is a (1 by 844) which means that I want to store each iteration of Classes.E_f./N into the prevalence matrix I have pre-defined. But I am running into error : Unable to perform assignment because the indices on the left side are not compatible with the size of the right side.
I know the sizes differs but I don't know how better to handle this.
0 Commenti
Risposta accettata
Steven Lord
il 18 Lug 2022
You have 4220 (= 844*5) eggs and an egg carton with 844 cups. How can you fit all 4,220 eggs into 844 cups (and no, scrambling the eggs is not an option in this metaphor.)
Either you need to cut down the amount of data you're trying to store in Prevalence(i, :) or "you're gonna need a bigger matrix."
I'd probably preallocate Prevalence to be an array of size [844 5 niter] and store the matrix of parameters in each page of the array, assuming that the size of Classes.E_f./N is always [844 5].
A = [1 2; 3 4];
Z = zeros(2, 2, 3);
for k = 1:3
fprintf("k is %d\n", k)
Z(:, :, k) = A^k
end
Più risposte (0)
Vedere anche
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!