run a function with 2 outputs multiple times

1 回表示 (過去 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 Center および File ExchangeScatter Plots についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by