Search code examples
matlabmatlab-deployment

detecting change in a binary column vector


I am new to matlab, I have a column vector with binary values I want to get the indices where the value changes from 0 to 1, and the indices when there is a change from 1 to 0.

This is my first question to Stackoverflow so am hoping to get the needed help, Thank you in advance.


Solution

  • Use diff with find.

    x = rand(1, 10) > 0.5
    
    % all changes
    find(diff(x)) + 1
    
    % 0 to 1 and 0 to 1 separately
    find(diff(x) == 1) + 1
    find(diff(x) == -1) + 1