MATLAB: Simple question

vector

I have a array of 1's and -1's. The data is in the format of S =[ 1 1 1 1 1 -1 -1 -1 -1 1 1 1 1]
It is always changing between 1 and -1. Could somebody give me code on how to quantify howmany time it changes from on to the other. Additionally is it possible somebody could give the average of 1's per time 1's occur and average -1's per time -1's occur.

Best Answer

using
ChangesN = find(diff(S))
would demonstrate where the value was changing and then to see how many time this happens:
length(ChangesN)