MATLAB: How to remove unwanted zeros from a vector

remove zeros in between non zero values

I have a column vector of the type [ 0 0 0 0 4 5 6 0 0 0 9 9 8 7 6 0 0 0 0 0 0 3 4 4 0 0 0]
Want to remove unwanted zeros to get the form [ 0 4 5 6 0 9 9 8 7 6 0 3 4 4 0]
i -e I don't wana remove all zeros or zeros in the beginning and end. find will not work I guess:

Best Answer

v = [ 0 0 0 0 4 5 6 0 0 0 9 9 8 7 6 0 0 0 0 0 0 3 4 4 0 0 0];
d = [1 diff(v)]; % 0's indicate when the value didn't change
x = d==0 & v==0; % Indexes where value is 0 and didn't change
v(x) = []; % Remove the indexes associated with x