<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/163278</link>
    <title>MATLAB Central Newsreader - operation with a vector of n elements</title>
    <description>Feed for thread: operation with a vector of n elements</description>
    <language>en-us</language>
    <copyright>&amp;copy;1994-2008 by The MathWorks, Inc.</copyright>
    <webmaster>webmaster@mathworks.com</webmaster>
    <generator>MATLAB Central Newsreader</generator>
    <docs>http://blogs.law.harvard.edu/tech/rss</docs>
    <ttl>60</ttl>
    <image>
      <title>The MathWorks</title>
      <url>http://www.mathworks.com/images/membrane_icon.gif</url>
    </image>
    <item>
      <pubDate>Wed, 06 Feb 2008 17:32:03 -0500</pubDate>
      <title>operation with a vector of n elements</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/163278#413343</link>
      <author>Marcelo Tames</author>
      <description>Hello, I just started studying matlab so my knowledge is &lt;br&gt;
little. I have this problem: Given a vector x with n &lt;br&gt;
elements, write a MATLAB function&lt;br&gt;
with x as input parameter to form the vector p with elements&lt;br&gt;
pk = x1*x2...xk1*xk+1...xn;&lt;br&gt;
&lt;br&gt;
that is, pk will contain the products of all the vector &lt;br&gt;
elements except the kth. Then vector P = [p1 p2 ... pn]&lt;br&gt;
&lt;br&gt;
I need help writing the program please&lt;br&gt;
Thanks a lot&lt;br&gt;
</description>
    </item>
    <item>
      <pubDate>Wed, 06 Feb 2008 17:58:45 -0500</pubDate>
      <title>Re: operation with a vector of n elements</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/163278#413353</link>
      <author>roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson)</author>
      <description>In article &amp;lt;focquj$ked$1@fred.mathworks.com&amp;gt;,&lt;br&gt;
Marcelo Tames &amp;lt;jmarcelo.tb@mathworks.com&amp;gt; wrote:&lt;br&gt;
&amp;gt;Hello, I just started studying matlab so my knowledge is &lt;br&gt;
&amp;gt;little. I have this problem: Given a vector x with n &lt;br&gt;
&amp;gt;elements, write a MATLAB function&lt;br&gt;
&amp;gt;with x as input parameter to form the vector p with elements&lt;br&gt;
&amp;gt;pk = x1*x2...xk1*xk+1...xn;&lt;br&gt;
&lt;br&gt;
&amp;gt;that is, pk will contain the products of all the vector &lt;br&gt;
&amp;gt;elements except the kth. Then vector P = [p1 p2 ... pn]&lt;br&gt;
&lt;br&gt;
P = prod(x) ./ x;&lt;br&gt;
&lt;br&gt;
Note: this has a subtle bug in it. You will need to test it&lt;br&gt;
to figure out the circumstances under which it can go wrong,&lt;br&gt;
and you will have to make adjustments so that it gives the&lt;br&gt;
right answer under those circumstances.&lt;br&gt;
&lt;br&gt;
-- &lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;"Any sufficiently advanced bug is indistinguishable from a feature."&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;-- Rich Kulawiec&lt;br&gt;
</description>
    </item>
    <item>
      <pubDate>Wed, 06 Feb 2008 18:12:01 -0500</pubDate>
      <title>Re: operation with a vector of n elements</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/163278#413360</link>
      <author>Roger Stafford</author>
      <description>"Marcelo Tames" &amp;lt;jmarcelo.tb@mathworks.com&amp;gt; wrote in message &amp;lt;focquj&lt;br&gt;
$ked$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; Hello, I just started studying matlab so my knowledge is &lt;br&gt;
&amp;gt; little. I have this problem: Given a vector x with n &lt;br&gt;
&amp;gt; elements, write a MATLAB function&lt;br&gt;
&amp;gt; with x as input parameter to form the vector p with elements&lt;br&gt;
&amp;gt; pk = x1*x2...xk1*xk+1...xn;&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; that is, pk will contain the products of all the vector &lt;br&gt;
&amp;gt; elements except the kth. Then vector P = [p1 p2 ... pn]&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; I need help writing the program please&lt;br&gt;
&amp;gt; Thanks a lot&lt;br&gt;
--------&lt;br&gt;
&amp;nbsp;&amp;nbsp;This has the earmark of homework, but if you can be sure that all elements &lt;br&gt;
of x are (accurate) non-zeros, here's an idea for you.  Take the product of all &lt;br&gt;
elements ('prod' function) and then divide it by x.  (Remember to use the dot &lt;br&gt;
"./" in the division operation to get element-by-element division rather than &lt;br&gt;
matrix division.)&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;If you face the possibility that one or more x elements might be zero, the &lt;br&gt;
problem becomes more difficult.  The above method would give you NaNs.  &lt;br&gt;
You can construct brute force nested for-loops to carry out the n different &lt;br&gt;
products, each of n-1 factors.  Or you can search for zeros in x and if you &lt;br&gt;
find more than one, set p to all zeros.  With just one zero in x you can set all &lt;br&gt;
but the corresponding element of p to zero and then use 'prod' to compute &lt;br&gt;
that one non-zero element of p.&lt;br&gt;
&lt;br&gt;
Roger Stafford&lt;br&gt;
&lt;br&gt;
</description>
    </item>
    <item>
      <pubDate>Wed, 06 Feb 2008 21:15:05 -0500</pubDate>
      <title>Re: operation with a vector of n elements</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/163278#413414</link>
      <author>Marcelo Tames</author>
      <description>I think I get what you mean, and yes it?s a homework that &lt;br&gt;
kept me thinking the whole afternoon, thanks fort the ideas &lt;br&gt;
</description>
    </item>
    <item>
      <pubDate>Thu, 20 Mar 2008 01:29:02 -0400</pubDate>
      <title>Re: operation with a vector of n elements</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/163278#421848</link>
      <author>Harish Jain</author>
      <description>"Marcelo Tames" &amp;lt;jmarcelo.tb@mathworks.com&amp;gt; wrote in message&lt;br&gt;
&amp;lt;fod80o$kni$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; I think I get what you mean, and yes it?s a homework that &lt;br&gt;
&amp;gt; kept me thinking the whole afternoon, thanks fort the ideas &lt;br&gt;
My homework is one step beyond this point. &lt;br&gt;
&lt;br&gt;
I need some of the all the element except itself and do not&lt;br&gt;
use division (/) operator (That means only use *&lt;br&gt;
(Multiplication).&lt;br&gt;
&lt;br&gt;
One step further from here is to create liner solution for&lt;br&gt;
minimum computations.&lt;br&gt;
&lt;br&gt;
Let me know your thoughts.&lt;br&gt;
&lt;br&gt;
-Harish Jain&lt;br&gt;
</description>
    </item>
    <item>
      <pubDate>Thu, 20 Mar 2008 01:30:05 -0400</pubDate>
      <title>Re: operation with a vector of n elements</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/163278#421849</link>
      <author>Harish Jain</author>
      <description>"Marcelo Tames" &amp;lt;jmarcelo.tb@mathworks.com&amp;gt; wrote in message&lt;br&gt;
&amp;lt;fod80o$kni$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; I think I get what you mean, and yes it?s a homework that &lt;br&gt;
&amp;gt; kept me thinking the whole afternoon, thanks fort the ideas &lt;br&gt;
My homework is one step beyond this point. &lt;br&gt;
&lt;br&gt;
I need some of the all the element except itself and do not&lt;br&gt;
use division (/) operator (That means only use *&lt;br&gt;
(Multiplication).&lt;br&gt;
&lt;br&gt;
One step further from here is to create liner solution for&lt;br&gt;
minimum computations.&lt;br&gt;
&lt;br&gt;
Let me know your thoughts.&lt;br&gt;
&lt;br&gt;
-Harish Jain&lt;br&gt;
</description>
    </item>
    <item>
      <pubDate>Thu, 20 Mar 2008 06:08:03 -0400</pubDate>
      <title>Re: operation with a vector of n elements</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/163278#421869</link>
      <author>Roger Stafford</author>
      <description>"Harish Jain" &amp;lt;hp_jain@yahoo.com&amp;gt; wrote in message &amp;lt;frseku$cg9&lt;br&gt;
$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; "Marcelo Tames" &amp;lt;jmarcelo.tb@mathworks.com&amp;gt; wrote in message&lt;br&gt;
&amp;gt; &amp;lt;fod80o$kni$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; &amp;gt; I think I get what you mean, and yes it?s a homework that &lt;br&gt;
&amp;gt; &amp;gt; kept me thinking the whole afternoon, thanks fort the ideas &lt;br&gt;
&amp;gt; My homework is one step beyond this point. &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; I need some of the all the element except itself and do not&lt;br&gt;
&amp;gt; use division (/) operator (That means only use *&lt;br&gt;
&amp;gt; (Multiplication).&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; One step further from here is to create liner solution for&lt;br&gt;
&amp;gt; minimum computations.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Let me know your thoughts.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; -Harish Jain&lt;br&gt;
-----------&lt;br&gt;
&amp;nbsp;&amp;nbsp;So--oo!  You want a "linear" solution - that is, an order(n) algorithm - and &lt;br&gt;
no division allowed?  Hmm!  Well, I will only give you a hint on this &lt;br&gt;
homework, because I wouldn't want to spoil things for you (and your &lt;br&gt;
classmates.)  In matlab there is a function called 'cumprod' that does &lt;br&gt;
wondrous, wondrous things and yet is only an order(n) operation.  Using this &lt;br&gt;
function appropriately, there is a way to fashion an algorithm that will solve &lt;br&gt;
your problem and still remain order(n).  Does this stir up the little grey cells &lt;br&gt;
sufficiently well?&lt;br&gt;
&lt;br&gt;
Roger Stafford&lt;br&gt;
&lt;br&gt;
</description>
    </item>
    <item>
      <pubDate>Thu, 20 Mar 2008 06:29:02 -0400</pubDate>
      <title>Re: operation with a vector of n elements</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/163278#421871</link>
      <author>Marcelo Tames</author>
      <description>"Harish Jain" &amp;lt;hp_jain@yahoo.com&amp;gt; wrote in message &lt;br&gt;
&amp;lt;frsemt$cti$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; "Marcelo Tames" &amp;lt;jmarcelo.tb@mathworks.com&amp;gt; wrote in &lt;br&gt;
message&lt;br&gt;
&amp;gt; &amp;lt;fod80o$kni$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; &amp;gt; I think I get what you mean, and yes it?s a homework &lt;br&gt;
that &lt;br&gt;
&amp;gt; &amp;gt; kept me thinking the whole afternoon, thanks fort the &lt;br&gt;
ideas &lt;br&gt;
&amp;gt; My homework is one step beyond this point. &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; I need some of the all the element except itself and do &lt;br&gt;
not&lt;br&gt;
&amp;gt; use division (/) operator (That means only use *&lt;br&gt;
&amp;gt; (Multiplication).&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; One step further from here is to create liner solution for&lt;br&gt;
&amp;gt; minimum computations.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Let me know your thoughts.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; -Harish Jain&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
well, I?ve finished with this one already, I did it using &lt;br&gt;
many conditionals and it worked perfectly, this one by the &lt;br&gt;
way solves the problem when there are one or more zero &lt;br&gt;
vector elements:&lt;br&gt;
&lt;br&gt;
function [Pk] = vector_Pk (x)&lt;br&gt;
if x==0          &lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Pk = [];&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;elseif length(x)== &lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Pk = 1;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;elseif sum((x==0))&amp;gt;1 %if &lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Pk = zeros(size(x));&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;elseif sum((x==0))==0&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Pk = zeros(size(x));&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Pk(find(x))=prod(x(find(x)))./x;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;else Pk = zeros(size(x));&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Pk(find(x==0))=prod(x(find(x)));&lt;br&gt;
end&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
I thought if you don?t have to use (/) then just change it &lt;br&gt;
to .*x^(-1), hope it helps. &lt;br&gt;
&lt;br&gt;
</description>
    </item>
  </channel>
</rss>
