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
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
"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
"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
"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
"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
"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:
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.