MATLAB: Efficient way to count the number of times a value changes to a particular value in an array

array

What is the best way to do the following:
count=0;
for i=2:length(x)
if x(i)==1 && x(i-1)~=1
count=count+1;
end
end

Best Answer

sum(x(2:end)==1 & x(1:end-1)~=1)