Thread Subject: sums and loops

Subject: sums and loops

From: D Jurdi

Date: 4 Jan, 2008 09:44:52

Message: 1 of 12

I am stuck writing a loop, any help??

The problem:
z is a vector of 100 numbers
i would be generating a col vector p which sums the first 5
elements of z then the second 5 elements and so on (non
overlapping) , z will have 20 elements.

I tried this but :(

for z=1:20
    for i=1:100;
        j=(i+2):100;
        p(z)=sum(sp(i:j));
        end
    end

end

thanks for any help

Subject: sums and loops

From: Huy

Date: 4 Jan, 2008 10:08:49

Message: 2 of 12

p = sum(reshape(z,[5 20]))'

Anh Huy Phan
RIKEN - BSI


"D Jurdi" <bocconiluck@yahoo.com> wrote in message
<flkv6k$fa$1@fred.mathworks.com>...
> I am stuck writing a loop, any help??
>
> The problem:
> z is a vector of 100 numbers
> i would be generating a col vector p which sums the first 5
> elements of z then the second 5 elements and so on (non
> overlapping) , z will have 20 elements.
>
> I tried this but :(
>
> for z=1:20
> for i=1:100;
> j=(i+2):100;
> p(z)=sum(sp(i:j));
> end
> end
>
> end
>
> thanks for any help

Subject: sums and loops

From: Dave

Date: 4 Jan, 2008 13:56:10

Message: 3 of 12


thanks for the reply, i explain the question further

p=[1 2 3 4 5 6 7 8 9]'

we need a code that sums elements in p for ex the first
three elements at a time resulting in a new column s

s=[6 15 24]'

(6=1+2+3 .. 15=4+5+6 ... 24= 7+8+9 ...)

I tried reshape in didnt work but thanks for the input




"Huy " <phananhhuy@mathworks.com> wrote in message
<fll0jh$7rt$1@fred.mathworks.com>...
> p = sum(reshape(z,[5 20]))'
>
> Anh Huy Phan
> RIKEN - BSI
>
>
> "D Jurdi" <bocconiluck@yahoo.com> wrote in message
> <flkv6k$fa$1@fred.mathworks.com>...
> > I am stuck writing a loop, any help??
> >
> > The problem:
> > z is a vector of 100 numbers
> > i would be generating a col vector p which sums the first 5
> > elements of z then the second 5 elements and so on (non
> > overlapping) , z will have 20 elements.
> >
> > I tried this but :(
> >
> > for z=1:20
> > for i=1:100;
> > j=(i+2):100;
> > p(z)=sum(sp(i:j));
> > end
> > end
> >
> > end
> >
> > thanks for any help
>

Subject: sums and loops

From: tpl@eng.cam.ac.uk (Tim Love)

Date: 4 Jan, 2008 14:02:13

Message: 4 of 12

"Dave " <bocconiluck@yahoo.com> writes:


>thanks for the reply, i explain the question further

>p=[1 2 3 4 5 6 7 8 9]'

>we need a code that sums elements in p for ex the first
>three elements at a time resulting in a new column s

>s=[6 15 24]'

>(6=1+2+3 .. 15=4+5+6 ... 24= 7+8+9 ...)

>I tried reshape in didnt work but thanks for the input

It might help if you showed us exactly what you tried.

Subject: sums and loops

From: Huy

Date: 4 Jan, 2008 14:09:55

Message: 5 of 12

To Dave:

You have vector p = [1:9]
and you need to generate vector s
   s(1) = p(1) + p(2) + p(3)
   s(2) = p(4) + p(5) + p(6)
   ...

but you don't want to use 'reshape' function???

Anh Huy Phan
RIKEN - BSI

"Dave " <bocconiluck@yahoo.com> wrote in message
<flldtq$1bh$1@fred.mathworks.com>...
>
> thanks for the reply, i explain the question further
>
> p=[1 2 3 4 5 6 7 8 9]'
>
> we need a code that sums elements in p for ex the first
> three elements at a time resulting in a new column s
>
> s=[6 15 24]'
>
> (6=1+2+3 .. 15=4+5+6 ... 24= 7+8+9 ...)
>
> I tried reshape in didnt work but thanks for the input
>
>
>
>
> "Huy " <phananhhuy@mathworks.com> wrote in message
> <fll0jh$7rt$1@fred.mathworks.com>...
> > p = sum(reshape(z,[5 20]))'
> >
> > Anh Huy Phan
> > RIKEN - BSI
> >
> >
> > "D Jurdi" <bocconiluck@yahoo.com> wrote in message
> > <flkv6k$fa$1@fred.mathworks.com>...
> > > I am stuck writing a loop, any help??
> > >
> > > The problem:
> > > z is a vector of 100 numbers
> > > i would be generating a col vector p which sums the first
5
> > > elements of z then the second 5 elements and so on (non
> > > overlapping) , z will have 20 elements.
> > >
> > > I tried this but :(
> > >
> > > for z=1:20
> > > for i=1:100;
> > > j=(i+2):100;
> > > p(z)=sum(sp(i:j));
> > > end
> > > end
> > >
> > > end
> > >
> > > thanks for any help
> >
>

Subject: sums and loops

From: Peter Bone

Date: 4 Jan, 2008 14:25:11

Message: 6 of 12

"Dave " <bocconiluck@yahoo.com> wrote in message
<flldtq$1bh$1@fred.mathworks.com>...
>
> thanks for the reply, i explain the question further
>
> p=[1 2 3 4 5 6 7 8 9]'
>
> we need a code that sums elements in p for ex the first
> three elements at a time resulting in a new column s
>
> s=[6 15 24]'
>
> (6=1+2+3 .. 15=4+5+6 ... 24= 7+8+9 ...)
>
> I tried reshape in didnt work but thanks for the input

reshape works fine

p = [1 2 3 4 5 6 7 8 9]';
blockSize = 3;
s = sum(reshape(p,[blockSize length(p)/blockSize]))'

Subject: sums and loops

From: Dave

Date: 4 Jan, 2008 14:27:06

Message: 7 of 12

I have millions of observations in P, i tried reshape but it
didnt work, the example i provided was just to illustrate
the question on small vectors.

Reshape is producing an error . i am looking for non
overlapping subsums over fixed interval as in the example i
provided.

Thanks a lot for your reponse



"Huy " <phananhhuy@mathworks.com> wrote in message
<fllenj$9i8$1@fred.mathworks.com>...
> To Dave:
>
> You have vector p = [1:9]
> and you need to generate vector s
> s(1) = p(1) + p(2) + p(3)
> s(2) = p(4) + p(5) + p(6)
> ...
>
> but you don't want to use 'reshape' function???
>
> Anh Huy Phan
> RIKEN - BSI
>
> "Dave " <bocconiluck@yahoo.com> wrote in message
> <flldtq$1bh$1@fred.mathworks.com>...
> >
> > thanks for the reply, i explain the question further
> >
> > p=[1 2 3 4 5 6 7 8 9]'
> >
> > we need a code that sums elements in p for ex the first
> > three elements at a time resulting in a new column s
> >
> > s=[6 15 24]'
> >
> > (6=1+2+3 .. 15=4+5+6 ... 24= 7+8+9 ...)
> >
> > I tried reshape in didnt work but thanks for the input
> >
> >
> >
> >
> > "Huy " <phananhhuy@mathworks.com> wrote in message
> > <fll0jh$7rt$1@fred.mathworks.com>...
> > > p = sum(reshape(z,[5 20]))'
> > >
> > > Anh Huy Phan
> > > RIKEN - BSI
> > >
> > >
> > > "D Jurdi" <bocconiluck@yahoo.com> wrote in message
> > > <flkv6k$fa$1@fred.mathworks.com>...
> > > > I am stuck writing a loop, any help??
> > > >
> > > > The problem:
> > > > z is a vector of 100 numbers
> > > > i would be generating a col vector p which sums the
first
> 5
> > > > elements of z then the second 5 elements and so on (non
> > > > overlapping) , z will have 20 elements.
> > > >
> > > > I tried this but :(
> > > >
> > > > for z=1:20
> > > > for i=1:100;
> > > > j=(i+2):100;
> > > > p(z)=sum(sp(i:j));
> > > > end
> > > > end
> > > >
> > > > end
> > > >
> > > > thanks for any help
> > >
> >
>

Subject: sums and loops

From: Dave

Date: 4 Jan, 2008 14:29:21

Message: 8 of 12

I have millions of observations in P, i tried reshape but it
didnt work, the example i provided was just to illustrate
the question on small vectors.

Reshape is producing an error . i am looking for non
overlapping subsums over fixed interval as in the example i
provided.

Thanks a lot for your reponse



"Huy " <phananhhuy@mathworks.com> wrote in message
<fllenj$9i8$1@fred.mathworks.com>...
> To Dave:
>
> You have vector p = [1:9]
> and you need to generate vector s
> s(1) = p(1) + p(2) + p(3)
> s(2) = p(4) + p(5) + p(6)
> ...
>
> but you don't want to use 'reshape' function???
>
> Anh Huy Phan
> RIKEN - BSI
>
> "Dave " <bocconiluck@yahoo.com> wrote in message
> <flldtq$1bh$1@fred.mathworks.com>...
> >
> > thanks for the reply, i explain the question further
> >
> > p=[1 2 3 4 5 6 7 8 9]'
> >
> > we need a code that sums elements in p for ex the first
> > three elements at a time resulting in a new column s
> >
> > s=[6 15 24]'
> >
> > (6=1+2+3 .. 15=4+5+6 ... 24= 7+8+9 ...)
> >
> > I tried reshape in didnt work but thanks for the input
> >
> >
> >
> >
> > "Huy " <phananhhuy@mathworks.com> wrote in message
> > <fll0jh$7rt$1@fred.mathworks.com>...
> > > p = sum(reshape(z,[5 20]))'
> > >
> > > Anh Huy Phan
> > > RIKEN - BSI
> > >
> > >
> > > "D Jurdi" <bocconiluck@yahoo.com> wrote in message
> > > <flkv6k$fa$1@fred.mathworks.com>...
> > > > I am stuck writing a loop, any help??
> > > >
> > > > The problem:
> > > > z is a vector of 100 numbers
> > > > i would be generating a col vector p which sums the
first
> 5
> > > > elements of z then the second 5 elements and so on (non
> > > > overlapping) , z will have 20 elements.
> > > >
> > > > I tried this but :(
> > > >
> > > > for z=1:20
> > > > for i=1:100;
> > > > j=(i+2):100;
> > > > p(z)=sum(sp(i:j));
> > > > end
> > > > end
> > > >
> > > > end
> > > >
> > > > thanks for any help
> > >
> >
>

Subject: sums and loops

From: Steven Lord

Date: 4 Jan, 2008 14:42:37

Message: 9 of 12


"Dave " <bocconiluck@yahoo.com> wrote in message
news:fllfnq$rh5$1@fred.mathworks.com...
>I have millions of observations in P, i tried reshape but it
> didnt work, the example i provided was just to illustrate
> the question on small vectors.

Why don't you generate a simple case the same size as your P vector:

sample = 1:1000000; % Replace 1000000 with the length of P

and post the exact call to RESHAPE that you used along with the error
message you received?

> Reshape is producing an error . i am looking for non
> overlapping subsums over fixed interval as in the example i
> provided.

Let me guess ... the error message RESHAPE gave you is:

??? Error using ==> reshape
To RESHAPE the number of elements must not change.

That's the most common error users run into with RESHAPE, in my experience.
That error occurs when the product of the sizes you used in your RESHAPE
call is not equal to the number of elements in the array you want to
RESHAPE. For instance:

>> x = 1:10;
>> y = reshape(x, 3, 4)

x has 10 elements, but you're trying to reshape it into a 3-by-4 matrix,
which would have 12 elements. You can't do that. You'd need to pad x out
to 12 elements in order to do this. For example, padding x with two NaN
values gives:

>> x = 1:10;
>> y = reshape([x NaN(1, 2)], 3, 4)

y =

     1 4 7 10
     2 5 8 NaN
     3 6 9 NaN

RESHAPE is one of the better ways to do what you want -- if you show us what
you tried, we can try to determine why it's not working for you.

--
Steve Lord
slord@mathworks.com


Subject: sums and loops

From: Peter Boettcher

Date: 4 Jan, 2008 14:47:31

Message: 10 of 12

"Dave " <bocconiluck@yahoo.com> writes:

> thanks for the reply, i explain the question further
>
> p=[1 2 3 4 5 6 7 8 9]'
>
> we need a code that sums elements in p for ex the first
> three elements at a time resulting in a new column s
>
> s=[6 15 24]'
>
> (6=1+2+3 .. 15=4+5+6 ... 24= 7+8+9 ...)
>
> I tried reshape in didnt work but thanks for the input

1: "How do I put in a nail?"
2: "Use a hammer"
1: "No, I want to put a nail in a piece of wood.
    The hammer didn't work. What else can I try?"

You need to be much more specific about how you tried to use the hammer,
and in what way it didn't work. It's still the right tool, even if it
didn't work for you the first time.

-Peter

Subject: sums and loops

From: Huy

Date: 4 Jan, 2008 15:09:34

Message: 11 of 12

I think the reason is the Number of elements in vector p is not
divided by 3 (or 5).

In this case, you could patch your array with zero numbers,
then use reshape function.

Anh Huy Phan
RIKEN - BSI



"Dave " <bocconiluck@yahoo.com> wrote in message
<fllfs1$2na$1@fred.mathworks.com>...
> I have millions of observations in P, i tried reshape but it
> didnt work, the example i provided was just to illustrate
> the question on small vectors.
>
> Reshape is producing an error . i am looking for non
> overlapping subsums over fixed interval as in the example i
> provided.
>
> Thanks a lot for your reponse
>
>
>

Subject: sums and loops

From: Dave

Date: 8 Jan, 2008 16:24:02

Message: 12 of 12


Thanks for all, it worked great :)

"Peter Bone" <peterbone@hotmail.com> wrote in message
<fllfk7$m4m$1@fred.mathworks.com>...
> "Dave " <bocconiluck@yahoo.com> wrote in message
> <flldtq$1bh$1@fred.mathworks.com>...
> >
> > thanks for the reply, i explain the question further
> >
> > p=[1 2 3 4 5 6 7 8 9]'
> >
> > we need a code that sums elements in p for ex the first
> > three elements at a time resulting in a new column s
> >
> > s=[6 15 24]'
> >
> > (6=1+2+3 .. 15=4+5+6 ... 24= 7+8+9 ...)
> >
> > I tried reshape in didnt work but thanks for the input
>
> reshape works fine
>
> p = [1 2 3 4 5 6 7 8 9]';
> blockSize = 3;
> s = sum(reshape(p,[blockSize length(p)/blockSize]))'
>

Tags for this Thread

Everyone's Tags:

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.

Tag Activity for This Thread
Tag Applied By Date/Time
sums loops Dave 4 Jan, 2008 06:48:02
sums Dave 4 Jan, 2008 04:45:00
loops Dave 4 Jan, 2008 04:45:00
rssFeed for this Thread
 

MATLAB Central Terms of Use

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Terms prior to use.

Contact us at files@mathworks.com