[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: help on calculating sum of combinations
From: |
Miquel Cabanas |
Subject: |
Re: help on calculating sum of combinations |
Date: |
Wed, 08 Oct 2003 10:12:31 +0200 |
User-agent: |
Mutt/1.3.28i |
hi,
On Wed, Oct 08, 2003 at 08:05:18AM +0530, sanjiv kapur wrote:
>
> from a matrix or an array,i want to calculate the sum of various
> combinations, i.e
> if A=[2, 3, 4,5]
> how do i get
> a1=2+3+4+5
> a2=2*3+2*4+2*5
> a3=2*3*4+2*3*5+2*4*5+3*4*5
> a4=2*3*4*5
> please guide me.
some hints
1. give a more detailed explanation of what you want to calculate,
that way you will increase your chances of getting the right answer
2. you say you are interested in some matrix calculations but then
you use a vector to illustrate the sort of calculations you want
to use. Because of this the answers below could be not what you
want, in such case give us a matrix example
> a1=2+3+4+5
use sum () to obtain the sum of elements along a certain dimension.
Default is column-wise sum.
octave:36> a1 = sum (A)
a1 = 14
> a4=2*3*4*5
use prod() to obtain the product of elements along a certain
dimension. Default is column-wise.
octave:37> a4 = prod (A)
a4 = 120
> a2=2*3+2*4+2*5
that's easy too, take the first element of A, i.e. "A(1)", and
multiply it element-by-element, i.e. ".*", by all remaining elements
of A, that is from A(2) to A(length(A)), i.e. "A(2:length(A))"
octave:38> a2 = sum( A(1) .* A(2:length(A)) )
a2 = 24
> a3=2*3*4+2*3*5+2*4*5+3*4*5
i'm afraid you will have to write a small program to do this
calculation.
Miquel
--
Miquel E Cabanas ------------------------------------------------------
SeRMN, Universitat Autonoma de Barcelona (address@hidden)
------------------------------------------o-oo--ooo---ooo--oo-o--------
-------------------------------------------------------------
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
-------------------------------------------------------------