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:
sum of consecutive integer

Subject: sum of consecutive integer

From: Tadros

Date: 9 May, 2011 21:42:06

Message: 1 of 11

Hi All;
I have a logical vector of zeros and ones, and I need to sum the consecutive ones and have the sum places at the end of each consecutive series of ones.
e.g.:
Input = [0 0 0 1 1 1 1 0 0 1 1 0 0 0 0 1 1 1]
the desired output
output = [0 0 0 0 0 0 4 0 0 0 2 0 0 0 0 0 0 3]

Thanks

Subject: sum of consecutive integer

From: Florin Neacsu

Date: 9 May, 2011 22:07:04

Message: 2 of 11

"Tadros " <triad@ualberta.ca> wrote in message <iq9n3e$68t$1@newscl01ah.mathworks.com>...
> Hi All;
> I have a logical vector of zeros and ones, and I need to sum the consecutive ones and have the sum places at the end of each consecutive series of ones.
> e.g.:
> Input = [0 0 0 1 1 1 1 0 0 1 1 0 0 0 0 1 1 1]
> the desired output
> output = [0 0 0 0 0 0 4 0 0 0 2 0 0 0 0 0 0 3]
>
> Thanks

Hi,

This is one way to do it
Input = [0 0 0 1 1 1 1 0 0 1 1 0 0 0 0 1 1 1];

Output=zeros(size(Input));

k=0;
for ii=1:numel(Input)
    if Input(ii)==0
        if k~=0
            Output(ii-1)=k;
            k=0;
        end
    elseif ii==numel(Input)
        Output(ii)=k;
    else
        k=k+1;
    end
end

Regards,
Florin

Subject: sum of consecutive integer

From: Florin Neacsu

Date: 9 May, 2011 22:23:02

Message: 3 of 11

Sorry, I have a mistake in the previous code

Try,

Input = [0 0 0 1 1 1 1 0 0 1 1 0 0 0 0 1 1 1 0 1];

Output=zeros(size(Input));

k=0;
for ii=1:numel(Input)
    if Input(ii)==0
        if k~=0
            Output(ii-1)=k;
            k=0;
        end
    else
        k=k+1;
    end
    
    if ii==numel(Input)
        Output(ii)=k;
    end
end

Florin

Subject: sum of consecutive integer

From: Roger Stafford

Date: 9 May, 2011 22:59:05

Message: 4 of 11

"Tadros " <triad@ualberta.ca> wrote in message <iq9n3e$68t$1@newscl01ah.mathworks.com>...
> Hi All;
> I have a logical vector of zeros and ones, and I need to sum the consecutive ones and have the sum places at the end of each consecutive series of ones.
> e.g.:
> Input = [0 0 0 1 1 1 1 0 0 1 1 0 0 0 0 1 1 1]
> the desired output
> output = [0 0 0 0 0 0 4 0 0 0 2 0 0 0 0 0 0 3]
>
> Thanks
- - - - - - - - -
  Here's another way. Let x be the input and y the desired output.

 t1 = [x,false]; t2 = [false,x];
 f1 = find(t1==1 & t2~=1); f2 = find(t1~=1 & t2==1);
 y = zeros(size(x));
 y(f2-1) = f2-f1;

Roger Stafford

Subject: sum of consecutive integer

From: Roger Stafford

Date: 9 May, 2011 23:32:05

Message: 5 of 11

"Roger Stafford" wrote in message <iq9rjp$g0n$1@newscl01ah.mathworks.com>...
> .......
> t1 = [x,false]; t2 = [false,x];
> f1 = find(t1==1 & t2~=1); f2 = find(t1~=1 & t2==1);
> y = zeros(size(x));
> y(f2-1) = f2-f1;
>
> Roger Stafford
- - - - - - - - -
  Assuming x really is a logical type I should have written:

 t1 = [x,false]; t2 = [false,x];
 f1 = find(t1&~t2); f2 = find(~t1&t2);
 y = zeros(size(x));
 y(f2-1) = f2-f1;

Roger Stafford

Subject: sum of consecutive integer

From: ImageAnalyst

Date: 9 May, 2011 23:39:00

Message: 6 of 11

And, just for fun and variety, here is the "imaging" way of doing it:

Input1 = [0 0 0 1 1 1 1 0 0 1 1 0 0 0 0 1 1 1]

[L, numRegions] = bwlabel(Input1) % Label regions
measurements=regionprops(L, 'area', 'PixelIdxList')
output = zeros(size(Input1)) % Preallocate output.
% Assign areas to last elements of each region.
for region = 1 : numRegions
    output(max(measurements(region).PixelIdxList))=
measurements(region).Area;
end
disp(output); % Show in command window.

6 "real" lines of code, like Roger's, but you need the Image
Processing Toolbox.

Subject: sum of consecutive integer

From: Roger Stafford

Date: 10 May, 2011 02:33:04

Message: 7 of 11

ImageAnalyst <imageanalyst@mailinator.com> wrote in message <b91af7fa-23ea-455b-af84-854676eb44de@j23g2000yqc.googlegroups.com>...
> 6 "real" lines of code, ......
- - - - - - - - - -
  I plead guilty. The temptation to combine short, related lines of code is often too much for me to resist.

Roger Stafford

Subject: sum of consecutive integer

From: ImageAnalyst

Date: 10 May, 2011 10:08:09

Message: 8 of 11

On May 9, 10:33 pm, "Roger Stafford"
<ellieandrogerxy...@mindspring.com.invalid> wrote:
> ImageAnalyst <imageanal...@mailinator.com> wrote in message <b91af7fa-23ea-455b-af84-854676eb4...@j23g2000yqc.googlegroups.com>...
> > 6 "real" lines of code, ......
>
> - - - - - - - - - -
>   I plead guilty.  The temptation to combine short, related lines of code is often too much for me to resist.
>
> Roger Stafford

-----------------------------------------------
I was also half way apologizing/explaining my code - often people
freak out and panic when they see my code because it's long but that's
just because I usually add lots of comments and split into two lines
what could be one just to make it easier to digest and understand. So
it may look twice as long, but in reality if they just go down a line
at a time, they'd see that it's really quite manageable.

Subject: sum of consecutive integer

From: Tadros

Date: 10 May, 2011 17:55:05

Message: 9 of 11

"Florin Neacsu" wrote in message <iq9pg6$bkq$1@newscl01ah.mathworks.com>...
> Sorry, I have a mistake in the previous code
>
> Try,
>
> Input = [0 0 0 1 1 1 1 0 0 1 1 0 0 0 0 1 1 1 0 1];
>
> Output=zeros(size(Input));
>
> k=0;
> for ii=1:numel(Input)
> if Input(ii)==0
> if k~=0
> Output(ii-1)=k;
> k=0;
> end
> else
> k=k+1;
> end
>
> if ii==numel(Input)
> Output(ii)=k;
> end
> end
>
> Florin

Thanks Florin a lot!
I tried it and it worked perfect as I exactly wanted!
Many thanks

Subject: sum of consecutive integer

From: Tadros

Date: 10 May, 2011 18:00:23

Message: 10 of 11

"Roger Stafford" wrote in message <iqa850$dk4$1@newscl01ah.mathworks.com>...
> ImageAnalyst <imageanalyst@mailinator.com> wrote in message <b91af7fa-23ea-455b-af84-854676eb44de@j23g2000yqc.googlegroups.com>...
> > 6 "real" lines of code, ......
> - - - - - - - - - -
> I plead guilty. The temptation to combine short, related lines of code is often too much for me to resist.
>
> Roger Stafford

Thanks Roger a lot!
this was really smart and saved me a lot of processing time!
my series is actually over 10,000 element long.
Thanks a lot

Subject: sum of consecutive integer

From: Tadros

Date: 10 May, 2011 18:06:02

Message: 11 of 11

ImageAnalyst <imageanalyst@mailinator.com> wrote in message <b91af7fa-23ea-455b-af84-854676eb44de@j23g2000yqc.googlegroups.com>...
> And, just for fun and variety, here is the "imaging" way of doing it:
>
> Input1 = [0 0 0 1 1 1 1 0 0 1 1 0 0 0 0 1 1 1]
>
> [L, numRegions] = bwlabel(Input1) % Label regions
> measurements=regionprops(L, 'area', 'PixelIdxList')
> output = zeros(size(Input1)) % Preallocate output.
> % Assign areas to last elements of each region.
> for region = 1 : numRegions
> output(max(measurements(region).PixelIdxList))=
> measurements(region).Area;
> end
> disp(output); % Show in command window.
>
> 6 "real" lines of code, like Roger's, but you need the Image
> Processing Toolbox.

Thanks a lot!
I'm feeling embarrased now! I was supposed to know this from the start, I have been doing some image processing long time ago, but I never thought of this problem that way!

Many thanks
Tadros

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