MATLAB: How can i do the scaling with negative integer.. i have done this with the positive integer

digital signal pro...digital signal processingMATLAB

a=0.1;
t=0:a:10;
t1=0:2*a:10;
x=exp(t);
subplot(2,1,1);
stem(t,x);
x1=exp(t1);
subplot(2,1,2);
stem(t1,x1)

Best Answer

  • To have negative steps with the colon operator, you need to make sure your first element is larger than your third element. Try out the following lines one by one to see the differences.
    1:0.5:3
    1:-0.5:3
    3:-0.5:1
    3:0.5:1
    It might be easiest to create your vector with a positive scale and reverse it if your scale is negative:
    t=0:abs(a):10;
    if a<0, t=t(end:-1:1);end
    Note that this can be different from 10:a:0 if 10 is not a multiple of a.
    You could also use a conditional in the first place:
    if a>=0
    t=0:a:10;
    else
    t=10:a:0;
    end