|
Brahim Hamadicharef <bhamadicharef@gmail.com> wrote in message <a84ed70e-f5e5-4a5d-a4a6-9f59e37591df@x5g2000prf.googlegroups.com>...
> MATLAB Profiler is saying that the while loop is taking a large amount
> of time
> Anyone see any ovbious optimization of my code ?
>
> Thank you in advance
>
> % VecSurfaceROC is a [nGridDim x nGridDim] matrix
> VecSurfaceROC = reshape(SurfaceROC, (nGridDim+1)^2, 1);
> SortedVecSurfaceROC = sort(VecSurfaceROC);
> SumVecSurfaceROC = sum(VecSurfaceROC);
> i = 1;
> CurrentArea = SortedVecSurfaceROC(1);
> while(CurrentArea < fCI*SumVecSurfaceROC)
> i = i + 1;
> CurrentArea = CurrentArea + SortedVecSurfaceROC(i);
> end
> v = SortedVecSurfaceROC(i);
Avoid the loop like this, using CUMSUM:
VecSurfaceROC = reshape(SurfaceROC, [], 1); % a row vector
SortedVecSurfaceROC = sort(VecSurfaceROC);
SumVecSurfaceROC = cumsum(VecSurfaceROC); % use cumsum
threshold = fCI .* SumVecSurfaceROC ;
q = SumVecSurfaceROC < threshold ;
idx = find(q,1,'last')
CurrentArea = SumVecSurfaceROC(idx)
% or
CurrentArea2 = sum(SortedVecSurfaceROC(q))
hth
Jos
|