Path: news.mathworks.com!not-for-mail
From: "Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid>
Newsgroups: comp.soft-sys.matlab
Subject: Re: operation with a vector of n elements
Date: Wed, 6 Feb 2008 18:12:01 +0000 (UTC)
Organization: The MathWorks, Inc.
Lines: 30
Message-ID: <foct9h$njq$1@fred.mathworks.com>
References: <focquj$ked$1@fred.mathworks.com>
Reply-To: "Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid>
NNTP-Posting-Host: webapp-05-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1202321521 24186 172.30.248.35 (6 Feb 2008 18:12:01 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Wed, 6 Feb 2008 18:12:01 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1187260
Xref: news.mathworks.com comp.soft-sys.matlab:449693


"Marcelo Tames" <jmarcelo.tb@mathworks.com> wrote in message <focquj
$ked$1@fred.mathworks.com>...
> Hello, I just started studying matlab so my knowledge is 
> little. I have this problem: Given a vector x with n 
> elements, write a MATLAB function
> with x as input parameter to form the vector p with elements
> pk = x1*x2...xk1*xk+1...xn;
> 
> that is, pk will contain the products of all the vector 
> elements except the kth. Then vector P = [p1 p2 ... pn]
> 
> I need help writing the program please
> Thanks a lot
--------
  This has the earmark of homework, but if you can be sure that all elements 
of x are (accurate) non-zeros, here's an idea for you.  Take the product of all 
elements ('prod' function) and then divide it by x.  (Remember to use the dot 
"./" in the division operation to get element-by-element division rather than 
matrix division.)

  If you face the possibility that one or more x elements might be zero, the 
problem becomes more difficult.  The above method would give you NaNs.  
You can construct brute force nested for-loops to carry out the n different 
products, each of n-1 factors.  Or you can search for zeros in x and if you 
find more than one, set p to all zeros.  With just one zero in x you can set all 
but the corresponding element of p to zero and then use 'prod' to compute 
that one non-zero element of p.

Roger Stafford