Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
How to sum to a threshold?

Subject: How to sum to a threshold?

From: Xuefei Cao

Date: 29 Apr, 2011 01:09:05

Message: 1 of 6

Hi everyone,

I'm trying to sum the elements of a vector, and the sum should stop when the accumulated sum accounts for, say 90% of the overall sum. For example: if V=[ 4 3 2 1], the program should stop when it takes 3 because (4+3+2)/sum(V)=90%.

Is there any elegant method other than using while loop?
 

Subject: How to sum to a threshold?

From: Xuefei Cao

Date: 29 Apr, 2011 01:30:19

Message: 2 of 6

"Xuefei Cao" <sophie.c1325@hotmail.com> wrote in message <ipd33g$5a$1@fred.mathworks.com>...
> Hi everyone,
>
> I'm trying to sum the elements of a vector, and the sum should stop when the accumulated sum accounts for, say 90% of the overall sum. For example: if V=[ 4 3 2 1], the program should stop when it takes 3 because (4+3+2)/sum(V)=90%.
>
> Is there any elegant method other than using while loop?
>
The solution I can think of is:
sum=0;
count=0;
while(sum<9)
    count=count+1;
    sum=sum+V(count);
end

....But could I improve it?

Subject: How to sum to a threshold?

From: Roger Stafford

Date: 29 Apr, 2011 02:51:04

Message: 3 of 6

"Xuefei Cao" <sophie.c1325@hotmail.com> wrote in message <ipd4bb$k83$1@fred.mathworks.com>...
> "Xuefei Cao" <sophie.c1325@hotmail.com> wrote in message <ipd33g$5a$1@fred.mathworks.com>...
> > Hi everyone,
> >
> > I'm trying to sum the elements of a vector, and the sum should stop when the accumulated sum accounts for, say 90% of the overall sum. For example: if V=[ 4 3 2 1], the program should stop when it takes 3 because (4+3+2)/sum(V)=90%.
> >
> > Is there any elegant method other than using while loop?
> >
> The solution I can think of is:
> sum=0;
> count=0;
> while(sum<9)
> count=count+1;
> sum=sum+V(count);
> end
>
> ....But could I improve it?
- - - - - - - -
  I don't think you could call the following an improvement. It has to take every possible partial sum and then check back to see how large each one is before deciding where it should have stopped, just as your while loop does. About the only claim one can make is that it's only two lines long.

 s = cumsum(V);
 sm = s(find(s<.90*s(end),1,'last'));

  Note: It is unwise to call any variable 'sum' because of a conflict with matlab' 'sum' function.

Roger Stafford

Subject: How to sum to a threshold?

From: ImageAnalyst

Date: 29 Apr, 2011 02:54:21

Message: 4 of 6

How about

% Generate sample data.
clc;
V = rand(200);

% Now find the index where it crosses 90%
cumulativeSum = cumsum(V) / sum(V);
indexAt90Percent = find(cumulativeSum >= 0.9, 1, 'first')

Subject: How to sum to a threshold?

From: Rune Allnor

Date: 29 Apr, 2011 07:38:48

Message: 5 of 6

On Apr 29, 3:09 am, "Xuefei Cao" <sophie.c1...@hotmail.com> wrote:
> Hi everyone,
>
> I'm trying to sum the elements of a vector, and the sum should stop when the accumulated sum accounts for, say 90% of the overall sum. For example: if V=[ 4 3 2 1], the program should stop when it takes 3 because (4+3+2)/sum(V)=90%.
>
> Is there any elegant method other than using while loop?

No.

Since you want 90% of the *total* sum, you need to comute that
first. So that's a for-loop. Once you know the total sum, you
need to search again for the element near the threshold,
which is best done using a while loop.

All of this assumes that the vector is already sorted or ordered
in whatever way is appropriate for the application at hand.

Rune

Subject: How to sum to a threshold?

From: Steven_Lord

Date: 29 Apr, 2011 13:48:48

Message: 6 of 6



"ImageAnalyst" <imageanalyst@mailinator.com> wrote in message
news:3eb9ca2c-b2e2-4d1d-8d57-2a1203138bc1@28g2000yqu.googlegroups.com...
> How about
>
> % Generate sample data.
> clc;
> V = rand(200);
>
> % Now find the index where it crosses 90%
> cumulativeSum = cumsum(V) / sum(V);

You don't need to sum V twice; the last element of cumsum(V) is the sum of
the elements of V. Roger took advantage of this in his approach. I like how
he multiplied instead of dividing in case the sum of V turned out to be 0.
But instead of checking for the last time the value was under the threshold
as Roger did, I think we should check for the first time it exceeds the
threshold.

% Admittedly very degenerate data
V = (-1).^(1:10);
C = cumsum(V);
firstPast90 = find(C >= 0.9*C(end), 1, 'first')
firstPast90ApproachIA = find(C/C(end) >= 0.9, 1, 'first')
firstPast90ApproachRoger = find(C < 0.9*C(end), 1, 'last')

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Tags for this Thread

No tags are associated with this thread.

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.

Contact us