help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: subsums


From: Mike Miller
Subject: Re: subsums
Date: Sun, 6 Feb 2005 11:20:40 -0600 (CST)

On Sun, 6 Feb 2005, Dmitri A. Sergatskov wrote:

Gorazd Brumen wrote:
Hi,

How do you form a vector of subsums of a certain vector without the for loop?
Example:
Let x = [1 2 3 4 5 6 7 8 9 10]
I want to get a vector that would have as entries
sum([1 2 3 4 5]) and sum ([6 7 8 9 10])?


octave:1> x = [1 2 3 4 5 6 7 8 9 10]
x =

 1   2   3   4   5   6   7   8   9  10

octave:2> y=reshape(x,5,2)
y =

 1   6
 2   7
 3   8
 4   9
 5  10

octave:3> z=sum(y)
z =

15  40


Or, in general, for even N:

N=10 (for example)
z=sum(reshape([1:N],N/2,2))
z =

  15  40


Mike



-------------------------------------------------------------
Octave is freely available under the terms of the GNU GPL.

Octave's home on the web:  http://www.octave.org
How to fund new projects:  http://www.octave.org/funding.html
Subscription information:  http://www.octave.org/archive.html
-------------------------------------------------------------



reply via email to

[Prev in Thread] Current Thread [Next in Thread]