How do I calculate the proportion of 1s in Vector I at each time step and how to store this data in a vector?
4 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
KJones
am 9 Okt. 2016
Bearbeitet: Massimo Zanetti
am 9 Okt. 2016
How do I calculate the proportion of 1s(infected) in my vector I at each loop. Is there a way of storing this information? as when I increase my matrix to 100x100 it will be helpful to have the proportion at each loop outputted somewhere which is easy to read.
A=[1,1,0;1,1,1;0,1,1] %Network Matrix
I=[1;0;0] %Vector of Infected at i=0
for i=1:2 %Calculates those infected at i=1 and i=2
I=A*I %Network Matrix times vector at the different times
I>0 %True/False Check: 0=not infected, 1=infected
end
Thank you
0 Kommentare
Akzeptierte Antwort
Massimo Zanetti
am 9 Okt. 2016
Bearbeitet: Massimo Zanetti
am 9 Okt. 2016
Given a vector of 1s and 0s, you can divide the number of 1s by the overall number of elements. The nnz function computes the number of non-zero elements in array, so:
I = [1,1,0,1,1,1,0,1,1] ;
n = numel(I);
proportion = nnz(I)/n;
0 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Particle & Nuclear Physics finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!