run a function with 2 outputs multiple times

조회 수: 7 (최근 30일)
Charlotte Mason
Charlotte Mason 2017년 8월 2일
댓글: Charlotte Mason 2017년 8월 3일
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

채택된 답변

ES
ES 2017년 8월 3일
편집: ES 2017년 8월 3일
for iLoop=1:100
[t,X] = SISep(adj, beta, gamma, nmax, I0);
tSim(iLoop)=t;
XSim(iLoop)=X;
end
  댓글 수: 1
Charlotte Mason
Charlotte Mason 2017년 8월 3일
This didn't work. But I put tSIm(iLoop,:)=t and XSim(iLoop,:,:)=X and I think it worked. thanks

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Scatter Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by