MATLAB: How to set x(i)=0 when i is negative

MATLABnegative index

Hi, I have two arrays x(n),y(n) and I want to calculate y(n)=2x(n)-3x(n-1)-.6814y(n-4) from n=0 to a specific integer. I know a negative index doesn't make sense. So I need to set x(i)=0,y(i)=0 when i is negative.
How can I do this?
Thanks in advance.
Chuan

Best Answer

Not possible. Sorry.
MATLAB does not allow indexing a variable with zero or negative indices. MATLAB uses origin 1 indexes. Period. Yes, you could define a separate class that DID allow negative indexes. Then you would need to supply subscripting tools, make sure that arithmetic was defined for your class, etc.
Of course, you CAN offset your index. Nothing stops you from doing that. Or you can just use a different equation for small n, where those terms would be zero anyway. You just need to be creative about how you work with the indexes.
For example, NOTHING stops you from writing the expression as:
y(n+4)=2*x(n+4)-3*x(n-3)-.6814*y(n)
As long as n starts at 1, WTP? Define the first few terms as zero. An index is just a number, as are the vectors involved. How you define them, how you treat them is what matters.