Thread Subject: Vector Math vs For Loop

Subject: Vector Math vs For Loop

From: happydude

Date: 17 Nov, 2009 21:31:19

Message: 1 of 7

 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))

Subject: Vector Math vs For Loop

From: Matt Fig

Date: 17 Nov, 2009 21:48:03

Message: 2 of 7

rand(1000) is not a vector.

Your code will error with
"In assignment A(I) = B, the number of elements in B and
 I must be the same."

If you post an working For loop, complete with numeric inputs, then you may get a better answer.

Subject: Vector Math vs For Loop

From: the cyclist

Date: 17 Nov, 2009 22:13:03

Message: 3 of 7

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

Subject: Vector Math vs For Loop

From: the cyclist

Date: 17 Nov, 2009 22:37:02

Message: 4 of 7

"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)

%%%

Subject: Vector Math vs For Loop

From: happydude

Date: 18 Nov, 2009 17:24:03

Message: 5 of 7

thank you so much for trying this problem...
i couldnt help but get a little confused sorry !

in your code you have
B=0.5
but B is actually a vector?

also in the equation the current value of "x" depends on a previous value of "x", which hasn't been asigned an initial value in the code?

Subject: Vector Math vs For Loop

From: the cyclist

Date: 18 Nov, 2009 22:37:05

Message: 6 of 7

"happydude " <anonymousse@hotmail.com> wrote in message <he1ajj$b7a$1@fred.mathworks.com>...
> thank you so much for trying this problem...
> i couldnt help but get a little confused sorry !
>
> in your code you have
> B=0.5
> but B is actually a vector?
>
> also in the equation the current value of "x" depends on a previous value of "x", which hasn't been asigned an initial value in the code?

happydude,

You are not being careful enough about notation. When I used the variable "B", 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.

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.

I hope that helps.

the cyclist

Subject: Vector Math vs For Loop

From: happydude

Date: 19 Nov, 2009 14:33:23

Message: 7 of 7

ah, sorry... still new, bit confused..

thanks for your help :)

Tags for this Thread

Everyone's Tags:

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.

Tag Activity for This Thread
Tag Applied By Date/Time
vector instead ... happydude 17 Nov, 2009 16:34:23
rssFeed for this Thread
 

MATLAB Central Terms of Use

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 Terms prior to use.

Contact us at files@mathworks.com