Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations KootK on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

matlab question

Status
Not open for further replies.

dakster

Chemical
Oct 15, 2009
1
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?
 
Replies continue below

Recommended for you

You need to use an array. Try this simple example. Type the following lines into the matlab terminal and see the result.

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



 
Newvector=Origvector*0;

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 for tips on how to make the best use of Eng-Tips.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor