Path: news.mathworks.com!not-for-mail
From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Vector Math vs For Loop
Date: Tue, 17 Nov 2009 22:37:02 +0000 (UTC)
Organization: PatientsLikeMe
Lines: 36
Message-ID: <hdv8ie$th$1@fred.mathworks.com>
References: <hdv4n7$la$1@fred.mathworks.com> <hdv75f$2s4$1@fred.mathworks.com>
Reply-To: <HIDDEN>
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 1258497422 945 172.30.248.35 (17 Nov 2009 22:37:02 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Tue, 17 Nov 2009 22:37:02 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1841757
Xref: news.mathworks.com comp.soft-sys.matlab:585964


"the cyclist" <thecyclist@gmail.com> wrote in message <hdv75f$2s4$1@fred.mathworks.com>...
> "happydude " <anonymousse@hotmail.com> wrote in message <hdv4n7$la$1@fred.mathworks.com>...
> >  is it possible to calculate X when
> > 
> > X(i) =X(i-1) + A * (B - X(i-1) )
> > 
> > using a vector script instead of a loop
> > 
> > **Importantly "x" is not a known vector, it is only given an initial value. 
> > "a" is a constant
> > "b" is a vector (for this example lets say its rand(1000))
> 
> You can do this in a one-liner using the FILTER function.  "help filter" 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 "x" is what the documentation calls "y", 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.

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 "help filter" according to the following scheme: (B,A,X,Y) --> (z,m,p,x).

I hope this helps rather than confuses you!

%%%
NUMBERTERMS = 10;

A = 0.3;
B = 0.5;

z = (A*B) * ones(NUMBERTERMS,1);

m = zeros(NUMBERTERMS,1);
m(1) = 1;
m(2) = -(1-A);

p = zeros(NUMBERTERMS,1);
p(1) = 1;

x = filter(z,m,p)

%%%