loops - Making sure values do not get overwritten in Matlab -


i modelling spread of disease , want make image plots first time infection occurs in pixel of matrix.

so got this:

for t=1:30         infected=calc_infected(susceptibles, disease, row, col);         if t < 8                            % infection occurs in first week in these pixels                                   lm(find(infected > 0))= 2                 elseif t>=8 && t<15                 % infection occurs in second week in these pixels             lm(find(infected > 0))= 3;         elseif t>=15 && t<22              lm(find(infected > 0))= 4;         elseif t>=22 && t<30              lm(find(infected > 0))= 5; end  

t time in days. lm map on plot data. infected matrix contains temporal evolution of infection. question is: want show time first infection occurs, how can make sure value pixel not overwritten when becomes infected second time, within time classification? e.g. if pixel has value of 2 because got infected @ t=5, gets infected again @ t=25, can overwritten value of 5.

thanks already!

i realized you're trying , problem is:) if understand correctly: lm array of same size infected, latter binary infection state of given cell, , lm shows segmented time-evolution of infection. each class of values of lm indicate part of map infected last, , want keep track of first infection time segment instead.

i suggest zero-initializing lm (which should anyway performance), check zeroness of lm when overwriting. elaborating on commented solution:

lm = zeros(row,col); % or size if not tmax = 30;  t=1:tmax    infected = calc_infected(susceptibles, disease, row, col);    inds = (infected>0) & (~lm);  % consider lm==0 values    switch t       case num2cell(1:7)          lm(inds)=2;       case num2cell(8:14)          lm(inds)=3;       case num2cell(15:21)          lm(inds)=4;       case num2cell(22:29)          lm(inds)=5;       otherwise          % if t==30!    end end 

note added ~lm logical indices logical and, short-hand lm==0, i.e. selects 0 indices in lm, need.


Comments

Popular posts from this blog

c++ - llvm function pass ReplaceInstWithInst malloc -

Cross-Compiling Linux Kernel for Raspberry Pi - ${CCPREFIX}gcc -v does not work -

java.lang.NoClassDefFoundError When Creating New Android Project -