Skip to Main Content Skip to Search
Login
File Exchange
MATLAB Newsgroup
Link Exchange
  Blogs  
 Contest 
MathWorks.com

Thread Subject: operation with a vector of n elements

Subject: operation with a vector of n elements

From: Marcelo Tames

Date: 06 Feb, 2008 17:32:03

Message: 1 of 8

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

Subject: Re: operation with a vector of n elements

From: roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson)

Date: 06 Feb, 2008 17:58:45

Message: 2 of 8

In article <focquj$ked$1@fred.mathworks.com>,
Marcelo Tames <jmarcelo.tb@mathworks.com> wrote:
>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]

P = prod(x) ./ x;

Note: this has a subtle bug in it. You will need to test it
to figure out the circumstances under which it can go wrong,
and you will have to make adjustments so that it gives the
right answer under those circumstances.

--
   "Any sufficiently advanced bug is indistinguishable from a feature."
   -- Rich Kulawiec

Subject: Re: operation with a vector of n elements

From: Roger Stafford

Date: 06 Feb, 2008 18:12:01

Message: 3 of 8

"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

Subject: Re: operation with a vector of n elements

From: Marcelo Tames

Date: 06 Feb, 2008 21:15:05

Message: 4 of 8

I think I get what you mean, and yes it?s a homework that
kept me thinking the whole afternoon, thanks fort the ideas

Subject: Re: operation with a vector of n elements

From: Harish Jain

Date: 20 Mar, 2008 01:29:02

Message: 5 of 8

"Marcelo Tames" <jmarcelo.tb@mathworks.com> wrote in message
<fod80o$kni$1@fred.mathworks.com>...
> I think I get what you mean, and yes it?s a homework that
> kept me thinking the whole afternoon, thanks fort the ideas
My homework is one step beyond this point.

I need some of the all the element except itself and do not
use division (/) operator (That means only use *
(Multiplication).

One step further from here is to create liner solution for
minimum computations.

Let me know your thoughts.

-Harish Jain

Subject: Re: operation with a vector of n elements

From: Harish Jain

Date: 20 Mar, 2008 01:30:05

Message: 6 of 8

"Marcelo Tames" <jmarcelo.tb@mathworks.com> wrote in message
<fod80o$kni$1@fred.mathworks.com>...
> I think I get what you mean, and yes it?s a homework that
> kept me thinking the whole afternoon, thanks fort the ideas
My homework is one step beyond this point.

I need some of the all the element except itself and do not
use division (/) operator (That means only use *
(Multiplication).

One step further from here is to create liner solution for
minimum computations.

Let me know your thoughts.

-Harish Jain

Subject: Re: operation with a vector of n elements

From: Roger Stafford

Date: 20 Mar, 2008 06:08:03

Message: 7 of 8

"Harish Jain" <hp_jain@yahoo.com> wrote in message <frseku$cg9
$1@fred.mathworks.com>...
> "Marcelo Tames" <jmarcelo.tb@mathworks.com> wrote in message
> <fod80o$kni$1@fred.mathworks.com>...
> > I think I get what you mean, and yes it?s a homework that
> > kept me thinking the whole afternoon, thanks fort the ideas
> My homework is one step beyond this point.
>
> I need some of the all the element except itself and do not
> use division (/) operator (That means only use *
> (Multiplication).
>
> One step further from here is to create liner solution for
> minimum computations.
>
> Let me know your thoughts.
>
> -Harish Jain
-----------
  So--oo! You want a "linear" solution - that is, an order(n) algorithm - and
no division allowed? Hmm! Well, I will only give you a hint on this
homework, because I wouldn't want to spoil things for you (and your
classmates.) In matlab there is a function called 'cumprod' that does
wondrous, wondrous things and yet is only an order(n) operation. Using this
function appropriately, there is a way to fashion an algorithm that will solve
your problem and still remain order(n). Does this stir up the little grey cells
sufficiently well?

Roger Stafford

Subject: Re: operation with a vector of n elements

From: Marcelo Tames

Date: 20 Mar, 2008 06:29:02

Message: 8 of 8

"Harish Jain" <hp_jain@yahoo.com> wrote in message
<frsemt$cti$1@fred.mathworks.com>...
> "Marcelo Tames" <jmarcelo.tb@mathworks.com> wrote in
message
> <fod80o$kni$1@fred.mathworks.com>...
> > I think I get what you mean, and yes it?s a homework
that
> > kept me thinking the whole afternoon, thanks fort the
ideas
> My homework is one step beyond this point.
>
> I need some of the all the element except itself and do
not
> use division (/) operator (That means only use *
> (Multiplication).
>
> One step further from here is to create liner solution for
> minimum computations.
>
> Let me know your thoughts.
>
> -Harish Jain


well, I?ve finished with this one already, I did it using
many conditionals and it worked perfectly, this one by the
way solves the problem when there are one or more zero
vector elements:

function [Pk] = vector_Pk (x)
if x==0
    Pk = [];
    elseif length(x)==
    Pk = 1;
        elseif sum((x==0))>1 %if
        Pk = zeros(size(x));
              elseif sum((x==0))==0
              Pk = zeros(size(x));
              Pk(find(x))=prod(x(find(x)))./x;
                  else Pk = zeros(size(x));
                  Pk(find(x==0))=prod(x(find(x)));
end


I thought if you don?t have to use (/) then just change it
to .*x^(-1), hope it helps.

Tags for this Thread

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.

rssFeed for this Thread

envelope graphic E-mail this page to a colleague

Public Submission Policy
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 Disclaimer prior to use.
Related Topics