MATLAB: How to calculate element distance in a vector

distancevector

hi guys i want to calculate the difference between vector element as [0 1 1 0 1 1 0] which should give 0-1=1,1-1=0,1-0=1,0-1=1,1-1=0,1-0=1,,, and the sum answer should be 4. how to do this. and what if there's 12 vector within one matrix.

Best Answer

It looks like you want the sum of the absolute value of the differential of those numbers? You get your first answer like this:
abs_diff = abs(diff([0 1 1 0 1 1 0]))
You would then sum these values. If all you want is the final answer, this is how to do it:
answer = sum(abs(diff([0 1 1 0 1 1 0])))