Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[SOLVED] define array to be 0 for all other values in Matlab

Status
Not open for further replies.

frznchckn

Member level 1
Joined
Jul 20, 2010
Messages
36
Helped
4
Reputation
8
Reaction score
4
Trophy points
1,288
Location
California
Activity points
1,521
I've just started learning dsp and am going through a book and doing some FIR filter problems.

I have a sequence x = [1 3 7 8 10]

>> x = [1 3 7 8 10]
x =
1 3 7 8 10
>> x(1)
ans =
1
>> x(5)
ans =
10
>> x(-1)
??? Index exceeds matrix dimensions.
>> x(6)
??? Index exceeds matrix dimensions.

Is there an easy way to have all invalid matrix dimensions to just return 0? I can obviously write a function to check if the value is within the valid matrix dimensions, but I'm looking for a built-in Matlab function.

Thanks.
 

Matlab does not use negative indexing. If you want to have some kind of safeguard, you need an index check every time you address your vector, such as:
Code:
if((n<1)||(n>5))
xn=0;
else
xn=x(n);
end
On the other hand, my preferred method, you can avoid all this complexity by calculating the index appropriately (just a little more work but worth the effort!).
 
So the answer is "no", I can't have Matlab assume 0 for all other index values.
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top