[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: help on calculating sum of combinations
From: |
Etienne Grossmann |
Subject: |
Re: help on calculating sum of combinations |
Date: |
Wed, 8 Oct 2003 18:27:33 +0100 |
User-agent: |
Mutt/1.3.28i |
Hello,
here is a very quick untested solution
function a = foo3 (A)
A = A(:)';
N = columns (A)
B = zeros (N,2^N); # Matrix of binary selection
for i = 1:N
B(i,:) = kron (kron (ones (1,2^(N-i)),[1 0]), ones (1,2^(i-1)));
end
B
b = sum (B)
C = (A'*ones(1,2^N)) .* B; # Compute products
C(find (C==0)) = 1;
c = prod (C)
a = zeros (1,N); # Sum products
for i = 1:2^N-1, a(b(i)) += c(i); end
end
hth
Etienne
On Wed, Oct 08, 2003 at 08:05:18AM +0530, sanjiv kapur wrote:
# hello
# i am a newbie for using octave (on rh9.0) ,though used a little matlab
# before.may i request help in solving the problem as under,in case a
# function has been developed by somebody.
# 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.
# thanks and regards
# sanjiv
#
# _________________________________________________________________
# Make fun prints Click here to order. http://www.kodakexpress.co.in
# Delivery anywhere in India.
#
#
#
# -------------------------------------------------------------
# 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
# -------------------------------------------------------------
#
--
Etienne Grossmann ------ http://www.isr.ist.utl.pt/~etienne
-------------------------------------------------------------
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
-------------------------------------------------------------