An efficient way to find the average difference between elements in an array

The name of hope does not confuse. This is just an example. I have a row vector as follows: [1 5 6]. I want to find the average difference between each element. The differences in this example are 4 and 1, so the average is 2.5. This is a small example. My line vectors can be very large. I am new to MatLab, so is there an efficient way to use the efficient MATLAB matrix / array processing to make it beautiful?

There is already a similar question about SOF, but this question is specifically for MATLAB!

Thank:)

EDIT: As requested by @gnovice, I need an absolute difference.

+5
source share
3 answers

diff

aveDiff = mean(diff(myVector))     %#(1)

>> v=[1 5 6]
v =
     1     5     6
>> mean(diff(v))
ans =
    2.5000

, @Jonas - .


@gnovice, @ @sevenless .

, abs (1)

aveDiff = mean(abs(diff(myVector)))     %#(2)
+8

array,

(array(end) - array(1))/(length(array)-1)

diff(array), array = [a b c d], <<24 > . (b-a+c-b+d-c)/3, (d-a)/3.

array = [1 5 6];

(array(end)-array(1))/2 

ans =
2.5
+4

If X is your vector, you can do

mean( X(2:end) - X(1:end-1) )
0
source

All Articles