Thread Subject: any ovbious optimization of my code ?

Subject: any ovbious optimization of my code ?

From: Brahim Hamadicharef

Date: 26 Nov, 2009 06:24:52

Message: 1 of 3

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);

Subject: any ovbious optimization of my code ?

From: Jos (10584)

Date: 26 Nov, 2009 09:02:03

Message: 2 of 3

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

Subject: any ovbious optimization of my code ?

From: Brahim Hamadicharef

Date: 26 Nov, 2009 09:25:30

Message: 3 of 3

Thank you Jos ... after putting my head around I came out with
a similar approach to your. I guess I will make use of profiler
more often in the future !

Best regards

Brahim

Tags for this Thread

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

rssFeed for this Thread

Contact us at files@mathworks.com