run a function with 2 outputs multiple times

3 vues (au cours des 30 derniers jours)
Charlotte Mason
Charlotte Mason le 2 Août 2017
Commenté : Charlotte Mason le 3 Août 2017
I'm sorry if this has been asked before, but I can't find any questions which give a simulation printing 2 different outputs from it. I have a function which gives me a vector t and a matrix X. I want to simulate t and X multiple times and store them i times to form two matrices (say i=100). I want the matrices to be called tSim and XSim. please help.
Here is my function:
if true
% function [t, X] = SISep(adj, beta, gamma, nmax, I0) % Adj = matrix of neighbours. beta = infection rate. gamma = recovery rate
n = length(adj);
ni = zeros(n, 1)'; % nodes infected
ni(1:I0)=1; % nodes infected, I0 nodes are infected
X = zeros(nmax-1, n); % matrix of zeros that will store the infected nodes
t = zeros(nmax-1, 1); % vector that stores the time of infection
X(1,:)=ni; % replaces first row with ni
k=2;
nuin=sum(ni);
while nuin > 0 && k < nmax
nuin = sum(ni); % number of infectives
r1ni=repmat((1-ni),[n,1]); % repeated vector in matrix
rni=repmat((ni)',[1,n]); % repeated vector in matrix
B=r1ni.*(adj).*rni; % need to multiple row by each element
nsipa= sum(sum(B)); % number of si pairs
totalRate = beta*nsipa + gamma*nuin;
pin = nsipa/(nsipa+nuin); % probability of infection
if rand<pin
[rows,cols]=find(B); %finds the rows and columns with an entry
infInd=cols(randi(length(cols))); % index of the individual infected. Should choose a random row to change
ni(infInd) = 1; % changes to 1
else [cols]=find(ni);
recInd= cols(randi(length(cols))); %recovered individuals
ni(recInd)= 0;
end
X(k,:)=ni;
t(k)=t(k-1)+exprnd(1/totalRate)';
k=k+1;
end
X=X;
t=t;
end
end
please help with tSim and XSim
if true
% [t,X] = SISep(adj, beta, gamma, nmax, I0);
tSim=?
XSim=?
end

Réponse acceptée

ES
ES le 3 Août 2017
Modifié(e) : ES le 3 Août 2017
for iLoop=1:100
[t,X] = SISep(adj, beta, gamma, nmax, I0);
tSim(iLoop)=t;
XSim(iLoop)=X;
end
  1 commentaire
Charlotte Mason
Charlotte Mason le 3 Août 2017
This didn't work. But I put tSIm(iLoop,:)=t and XSim(iLoop,:,:)=X and I think it worked. thanks

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Scatter Plots dans Help Center et File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by