<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/266153</link>
    <title>MATLAB Central Newsreader - Vector Math vs For Loop</title>
    <description>Feed for thread: Vector Math vs For Loop</description>
    <language>en-us</language>
    <copyright>&amp;copy;1994-2012 by 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>MathWorks</title>
      <url>http://www.mathworks.com/images/membrane_icon.gif</url>
    </image>
    <item>
      <pubDate>Tue, 17 Nov 2009 21:31:19 -0500</pubDate>
      <title>Vector Math vs For Loop</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/266153#695597</link>
      <author>happydude </author>
      <description>&amp;nbsp;is it possible to calculate X when&lt;br&gt;
&lt;br&gt;
X(i) =X(i-1) + A * (B - X(i-1) )&lt;br&gt;
&lt;br&gt;
using a vector script instead of a loop&lt;br&gt;
&lt;br&gt;
**Importantly &quot;x&quot; is not a known vector, it is only given an initial value. &lt;br&gt;
&quot;a&quot; is a constant&lt;br&gt;
&quot;b&quot; is a vector (for this example lets say its rand(1000))</description>
    </item>
    <item>
      <pubDate>Tue, 17 Nov 2009 21:48:03 -0500</pubDate>
      <title>Re: Vector Math vs For Loop</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/266153#695605</link>
      <author>Matt Fig</author>
      <description>rand(1000) is not a vector.  &lt;br&gt;
&lt;br&gt;
Your code will error with &lt;br&gt;
&quot;In assignment A(I) = B, the number of elements in B and&lt;br&gt;
&amp;nbsp;I must be the same.&quot;&lt;br&gt;
&lt;br&gt;
If you post an working For loop, complete with numeric inputs, then you may get a better answer.</description>
    </item>
    <item>
      <pubDate>Tue, 17 Nov 2009 22:13:03 -0500</pubDate>
      <title>Re: Vector Math vs For Loop</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/266153#695614</link>
      <author>the cyclist</author>
      <description>&quot;happydude &quot; &amp;lt;anonymousse@hotmail.com&amp;gt; wrote in message &amp;lt;hdv4n7$la$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt;  is it possible to calculate X when&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; X(i) =X(i-1) + A * (B - X(i-1) )&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; using a vector script instead of a loop&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; **Importantly &quot;x&quot; is not a known vector, it is only given an initial value. &lt;br&gt;
&amp;gt; &quot;a&quot; is a constant&lt;br&gt;
&amp;gt; &quot;b&quot; is a vector (for this example lets say its rand(1000))&lt;br&gt;
&lt;br&gt;
You can do this in a one-liner using the FILTER function.  &quot;help filter&quot; for details.  Be extremely careful in the notation, because the documentation coincidentally uses the same variable names as you have, but with different meanings!  Most importantly, I think what you are calling &quot;x&quot; is what the documentation calls &quot;y&quot;, which is the output of the filter function.  It is very easy to get confused, but I am 95% you can do exactly what you want with this function.</description>
    </item>
    <item>
      <pubDate>Tue, 17 Nov 2009 22:37:02 -0500</pubDate>
      <title>Re: Vector Math vs For Loop</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/266153#695617</link>
      <author>the cyclist</author>
      <description>&quot;the cyclist&quot; &amp;lt;thecyclist@gmail.com&amp;gt; wrote in message &amp;lt;hdv75f$2s4$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; &quot;happydude &quot; &amp;lt;anonymousse@hotmail.com&amp;gt; wrote in message &amp;lt;hdv4n7$la$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; &amp;gt;  is it possible to calculate X when&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; X(i) =X(i-1) + A * (B - X(i-1) )&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; using a vector script instead of a loop&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; **Importantly &quot;x&quot; is not a known vector, it is only given an initial value. &lt;br&gt;
&amp;gt; &amp;gt; &quot;a&quot; is a constant&lt;br&gt;
&amp;gt; &amp;gt; &quot;b&quot; is a vector (for this example lets say its rand(1000))&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; You can do this in a one-liner using the FILTER function.  &quot;help filter&quot; for details.  Be extremely careful in the notation, because the documentation coincidentally uses the same variable names as you have, but with different meanings!  Most importantly, I think what you are calling &quot;x&quot; is what the documentation calls &quot;y&quot;, which is the output of the filter function.  It is very easy to get confused, but I am 95% you can do exactly what you want with this function.&lt;br&gt;
&lt;br&gt;
Hoping I got this right  Here is some code that should work for you.  Again, be really careful about notation.  In my code snippet, I retained YOUR definition of A and B, and I changed the notation in &quot;help filter&quot; according to the following scheme: (B,A,X,Y) --&amp;gt; (z,m,p,x).&lt;br&gt;
&lt;br&gt;
I hope this helps rather than confuses you!&lt;br&gt;
&lt;br&gt;
%%%&lt;br&gt;
NUMBERTERMS = 10;&lt;br&gt;
&lt;br&gt;
A = 0.3;&lt;br&gt;
B = 0.5;&lt;br&gt;
&lt;br&gt;
z = (A*B) * ones(NUMBERTERMS,1);&lt;br&gt;
&lt;br&gt;
m = zeros(NUMBERTERMS,1);&lt;br&gt;
m(1) = 1;&lt;br&gt;
m(2) = -(1-A);&lt;br&gt;
&lt;br&gt;
p = zeros(NUMBERTERMS,1);&lt;br&gt;
p(1) = 1;&lt;br&gt;
&lt;br&gt;
x = filter(z,m,p)&lt;br&gt;
&lt;br&gt;
%%%</description>
    </item>
    <item>
      <pubDate>Wed, 18 Nov 2009 17:24:03 -0500</pubDate>
      <title>Re: Vector Math vs For Loop</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/266153#695828</link>
      <author>happydude </author>
      <description>thank you so much for trying this problem... &lt;br&gt;
i couldnt help but get a little confused sorry !&lt;br&gt;
&lt;br&gt;
in your code you have &lt;br&gt;
B=0.5&lt;br&gt;
but B is actually a vector? &lt;br&gt;
&lt;br&gt;
also in the equation the current value of &quot;x&quot; depends on a previous value of &quot;x&quot;, which hasn't been asigned an initial value in the code?</description>
    </item>
    <item>
      <pubDate>Wed, 18 Nov 2009 22:37:05 -0500</pubDate>
      <title>Re: Vector Math vs For Loop</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/266153#695900</link>
      <author>the cyclist</author>
      <description>&quot;happydude &quot; &amp;lt;anonymousse@hotmail.com&amp;gt; wrote in message &amp;lt;he1ajj$b7a$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; thank you so much for trying this problem... &lt;br&gt;
&amp;gt; i couldnt help but get a little confused sorry !&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; in your code you have &lt;br&gt;
&amp;gt; B=0.5&lt;br&gt;
&amp;gt; but B is actually a vector? &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; also in the equation the current value of &quot;x&quot; depends on a previous value of &quot;x&quot;, which hasn't been asigned an initial value in the code?&lt;br&gt;
&lt;br&gt;
happydude,&lt;br&gt;
&lt;br&gt;
You are not being careful enough about notation.  When I used the variable &quot;B&quot;, that is because YOU called a (scalar) variable B.  This is NOT the same as the B used in the MATLAB documentation, which is indeed a vector as you point out.&lt;br&gt;
&lt;br&gt;
Also, the filter function is more general than you need, with two separate vector inputs (which are called x and y in the documentation).  You have have only one vector.  The starting value of your vector is encoded into the inputs to the function.&lt;br&gt;
&lt;br&gt;
I hope that helps.&lt;br&gt;
&lt;br&gt;
the cyclist</description>
    </item>
    <item>
      <pubDate>Thu, 19 Nov 2009 14:33:23 -0500</pubDate>
      <title>Re: Vector Math vs For Loop</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/266153#696063</link>
      <author>happydude </author>
      <description>ah, sorry... still new, bit confused..&lt;br&gt;
&lt;br&gt;
thanks for your help :)</description>
    </item>
  </channel>
</rss>

