Saving vectors from a loop into a matrix

조회 수: 16 (최근 30일)
KJones
KJones 2016년 11월 12일
댓글: Changoleon 2016년 11월 13일
I have this code that I made and I would like to save my final vector as a row in a matrix as I would like to plot the averages. I would like this matrix to consist of each row representing a run at a different H value where the H values change from R>0.1 up until R>0.9. How can I do this automatically because I don't want to change my matrix A so it would need to be done in one run?
A=createsrandomnetwork(100,5); %Network Matrix
I=zeros(100,1);
positions=[1,1];
I(positions)=1;
v=zeros(1,100); %Vector which proportion value will be saved into
n=numel(I); %n=number of array elements
v(1)=nnz(I)/n; %At time 0 the proportion infected (nnz=number of non zero elements in I)
for i=1:100
a = rand(100);
R = triu(a) + triu(a,1)';
H=R>0.5; %Matrix with % chance of passing on infection
P=A.*H; %New Network Matrix including the probability
P(logical(eye(size(P)))) = 0; %makes sure the diagonal is 0 then people can recover
I=P*I; %New Network Matrix times the Vector of Infected
v(i)=nnz(I)/n %Proportion infected at time i
end
Thank you

답변 (1개)

Changoleon
Changoleon 2016년 11월 13일
편집: Walter Roberson 2016년 11월 13일
Hi. This is a very simplified solution, I believe you should be able to modify this code.
h=5;
for i=1:h
x(i) = i * 10;
s(i,:) = [i,x(i)];
end
Sina
  댓글 수: 2
KJones
KJones 2016년 11월 13일
Thank you, I have managed to get it so it saves in the different columns each time but it creates a different matrix.
A=createsrandomnetwork(20,3);
for k= 1:9
v=zeros(1,20); %Vector which proportion value will be saved into
n=numel(I);
I=zeros(20,1);
positions=[1,1];
I(positions)=1;
a = rand(20);
R = triu(a) + triu(a,1)';
x = k;
H=R>x/10;
for i=1:20
P=A.*H;
P(logical(eye(size(P)))) = 0; %makes sure the diagonal is 0 then people can recover
I=P*I;
v(i)=nnz(I)/n
end
s=zeros(20,9);
s(:,k)=v
end
How do I make it so each vector saves to the same matrix, so I can have a complete matrix at the end?
Thank you again.
Changoleon
Changoleon 2016년 11월 13일
Hi Kjones.
Could you explain your question with a simplified example? I am having trouble understanding what the problem is.
Yours Sina

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

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by