Search code examples
arraysmatlabintegersubtraction

Subtracting an array from an integer in MATLAB


I think I'm having a hard time understanding the code below in MATLAB. I've expected A to be [3,2,1,0] but it actually returned [3,4]. Why does this happen?

numberpoints = 1;
x = 4;

A = x-numberpoints:x;

Solution

  • This is what it's doing:

    > (x-numberpoints):x % (4-1):4 or 3:4
    
    ans =
       3   4
    

    To get your expected output:

    > x-(numberpoints:x) % 4-(1:4)
    
    ans =
       3   2   1   0