Info
La pregunta está cerrada. Vuélvala a abrir para editarla o responderla.
Simple Question about Optimization of Nested IF-FOR loops
    7 visualizaciones (últimos 30 días)
  
       Mostrar comentarios más antiguos
    
Does any one know how to optimize this code so that it runs faster:
    for i=1:iNZ;
        if iPointsinSlice>0;
            for m=1:iNX;
                for l=1:iNY;
                    if SliceMaskUr(m,l)==1;
                        DoseCubeU(m+(l-1)*iNX+i*iNX*iNY)=100*SumDose(m,l,i)/RX_Dose;
                    end
                end
            end
        end
    end
Your help is much appreciated! Thanks a lot!
0 comentarios
Respuestas (5)
  Roger Stafford
      
      
 el 17 de Jun. de 2013
        Is 'iPointsinSlice' a scalar? If so and if it is not positive, nothing will happen here.
Next, don't you mean "m+(l-1)*iNX+(i-1)*iNX*iNY" as the index to 'DoseCubeU'? As it stands it will vary from 1+iNX*iNY to iNX*iNY*iNZ+iNX*iNY. Assuming my guess is correct, do this:
 if iPointsinSlice > 0
   t = repmat(SliceMaskUr==1,1,1,iNZ);
   DoseCubeU(t) = (100/RX_Dose)*SumDose(t);
 end
In case 'iPointsinSlice' is not a scalar you will have to correct your code before we can see how to handle it.
1 comentario
  Roger Stafford
      
      
 el 17 de Jun. de 2013
        Sorry! It should have been:
 t = repmat(SliceMaskUr==1,[1,1,iNZ]);
1 comentario
  Andrei Bobrov
      
      
 el 18 de Jun. de 2013
        
      Editada: Andrei Bobrov
      
      
 el 19 de Jun. de 2013
  
      DoseCubeU = bsxfun(@times,100*SumDose/RX,SliceMaskUr==1);
ADD
s = SliceMaskUr(end:-1:1,end:-1:1);
tt = cumsum(cumsum(s),2);
t1 = flipud(any(tt,2));
t2 = fliplr(any(tt));
DoseCubeU = bsxfun(@times,100*SumDose(t1,t2,:)/RX_Dose,SliceMaskUr(t1,t2));
4 comentarios
  Iain
      
 el 18 de Jun. de 2013
        Try:
 DoseCubeU(NY,NX,NZ) = 0; % or any suitable initialisation
 if iPointsinSlice>0;
  DoseCubeU(SliceMaskUr(m,l)==1,:) = 100*SumDose(SliceMaskUr(m,l)==1,:);
 end
La pregunta está cerrada.
Ver también
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!



