how to vectorize it Classic List Threaded 5 messages Open this post in threaded view
|

how to vectorize it

 hi, see this code:  input for example:  ris=[1 0 0;0 1 0;1 0 0; 0 1 1]  tit=[0 1 0 0]  tic  bb=0;  for hh=1:columns(ris)   bb=0;   for gg=1:rows(ris)     if tit(gg)          bb=0;         if ris(gg,hh)             bb=1;          endif     endif      ris(gg,hh)=bb;      endfor  endfor  toc     it's possible to avoid loop? -- Sent from: http://octave.1599824.n4.nabble.com/Octave-General-f1599825.html
Open this post in threaded view
|

Re: how to vectorize it

 shivax wrote > hi, see this code: > >  input for example: > >  ris=[1 0 0;0 1 0;1 0 0; 0 1 1] >  tit=[0 1 0 0] > >  tic >  bb=0; >  for hh=1:columns(ris) >   bb=0; >   for gg=1:rows(ris) >     if tit(gg) >          bb=0; >         if ris(gg,hh) >             bb=1;     >      endif >     endif >      ris(gg,hh)=bb;   >    endfor >  endfor >  toc >   >   > > it's possible to avoid loop? > > > > -- > Sent from: > http://octave.1599824.n4.nabble.com/Octave-General-f1599825.htmlHere is what I understand of your very interesting problem.  Each row in the tit vector is associated with a row in the ris matrix.  All ris rows up until the first tit row that equals 1 should be set to zero.  When a tit row = 1, the associated row in the ris vector should be copied and over write all ris rows until tit row equals 1 again. I haven't been able to solve the problem, but I think repmat and find might be helpful. find(tit==1) will find the index of all the rows in tit that are equal to 1 and repmat(ris(row,:),row,1) can be used to repeat a specific row in ris. If you can find the difference between the row index values of tit==1, you can determine how many times to repeat a specific row.   If you find out how to vectorize this problem, could you please post the solution. Thanks, Stephen -- Sent from: http://octave.1599824.n4.nabble.com/Octave-General-f1599825.html
Open this post in threaded view
|

Re: how to vectorize it

 On Sun, Jul 21, 2019 at 5:51 PM shall689 <[hidden email]> wrote:shivax wrote > hi, see this code: > >  input for example: > >  ris=[1 0 0;0 1 0;1 0 0; 0 1 1] >  tit=[0 1 0 0] > >  tic >  bb=0; >  for hh=1:columns(ris) >   bb=0; >   for gg=1:rows(ris) >     if tit(gg) >          bb=0; >         if ris(gg,hh) >             bb=1;    >      endif >     endif >      ris(gg,hh)=bb;    >    endfor >  endfor >  toc >  >  > > it's possible to avoid loop? > > > > -- > Sent from: > http://octave.1599824.n4.nabble.com/Octave-General-f1599825.html If you explain what you are trying to do, there might be a better way.  Here is what I understand of your very interesting problem.  Each row in the tit vector is associated with a row in the ris matrix.  All ris rows up until the first tit row that equals 1 should be set to zero.  When a tit row = 1, the associated row in the ris vector should be copied and over write all ris rows until tit row equals 1 again. I haven't been able to solve the problem, but I think repmat and find might be helpful. find(tit==1) will find the index of all the rows in tit that are equal to 1 and repmat(ris(row,:),row,1) can be used to repeat a specific row in ris. If you can find the difference between the row index values of tit==1, you can determine how many times to repeat a specific row.  If you find out how to vectorize this problem, could you please post the solution. Thanks, Stephen -- Sent from: http://octave.1599824.n4.nabble.com/Octave-General-f1599825.html -- DAS 