matlab question
matlab question
(OP)
I am a Matlab newbie, and have been going through a Matlab guide as well as the help menus for about a week now, but I can't figure out how to write my first real script. I have a 1024x1 vector (actually I have a couple hundred of these), and I want to create a new vector wherein each element is a function of the preceding element and the corresponding element in the original vector. Specifically:
New Vector Element (i) = 0.55*Orig Vector Element (i-1) + 0.45*New Vector Element (i-1)
I know I need a 'for' loop, but I guess the thing I'm stuck on is how to tell Matlab to use the preceding value in the vector being populated. Help?
New Vector Element (i) = 0.55*Orig Vector Element (i-1) + 0.45*New Vector Element (i-1)
I know I need a 'for' loop, but I guess the thing I'm stuck on is how to tell Matlab to use the preceding value in the vector being populated. Help?





RE: matlab question
a = [1,2,3,4,5]
a(1)
a(2)
n = 4;
a(n)
a(n-1)
As you should notice, you defined an array called a and referenced the array elements by the index and that the index can be another variable or an expression.
You are correct, that you can use a for loop to index i over a range. For example:
for i = 2:5;
b(i) = a(i) + 0.5*a(i-1)
endfor
This creates a new array b = (1 + .5z^-1)*a
RE: matlab question
for i=2:length(Newvector)
Newvector(i)=.45*Newvector(i-1)+.55*Origvector(i-1);
end
Might need to transpose origvector beforehand. also note i=1 problem.
Cheers
Greg Locock
SIG:Please see FAQ731-376: Eng-Tips.com Forum Policies for tips on how to make the best use of Eng-Tips.