Simple Question about Optimization of Nested IF-FOR loops
정보
이 질문은 마감되었습니다. 편집하거나 답변을 올리려면 질문을 다시 여십시오.
이전 댓글 표시
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
답변 (5개)
Roger Stafford
2013년 6월 17일
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
Mohsen
2013년 6월 17일
Roger Stafford
2013년 6월 17일
Sorry! It should have been:
t = repmat(SliceMaskUr==1,[1,1,iNZ]);
댓글 수: 1
Mohsen
2013년 6월 18일
Mohsen
2013년 6월 17일
댓글 수: 4
Roger Stafford
2013년 6월 18일
No, I wouldn't expect it to give the same result if you still use
DoseCubeU(m+(l-1)*iNX+i*iNX*iNY)
As I already pointed out, that indexes DoseCubeU starting, not with 1 but with 1+iNX*iNY and ends not with iNX*iNY*iNZ but iNX*iNY*iNZ+iNX*iNY. In my modification I assumed you didn't want to do that. Note that this has nothing to do with reshaping SumDose. It should work in that code without being reshaped. Using t as a single logical index will cause it to be interpreted as a linear index. Similarly in DoseCubeU the use of the single logical vector t will be interpreted by DoseCubeU as a linear index. Note that I have assumed that both DoseCubeU and SumDose are of sizes iNX-by-iNY-by-iNZ.
Mohsen
2013년 6월 18일
Mohsen
2013년 6월 18일
Mohsen
2013년 6월 18일
Andrei Bobrov
2013년 6월 18일
편집: Andrei Bobrov
2013년 6월 19일
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
Mohsen
2013년 6월 18일
Mohsen
2013년 6월 18일
Mohsen
2013년 6월 18일
Andrei Bobrov
2013년 6월 19일
Corrected. See ADD part in my answer.
Iain
2013년 6월 18일
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
이 질문은 마감되었습니다.
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!