How to subtract a vector element without a loop?

Discussion in 'Chit Chat' started by blueraincap, Feb 1, 2020.

  1. I am totally new to vectorised language, so please enlighten me. For below (Octave), I want to subtract element i from the element before it (i-1), ie they are cumulative and i want to see the incremental. How can I do it without using a loop.

    Code:
    >> x
    x =
    
       30
       50
       55
       68
       85
       86
    
    >> for i=rows(x):-1:2
    x(i,1) = x(i,1) - x(i-1,1);
    end
    >> x
    x =
    
       30
       20
        5
       13
       17
        1