查找数组中元素之间平均差的有效方法

查找数组中元素之间平均差的有效方法

本文介绍了查找数组中元素之间平均差的有效方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

希望标题不会造成混淆.通过示例显示很简单.我有一个像这样的行向量:[1 5 6].我想找到每个元素之间的平均差异.在此示例中,差异为4和1,因此平均值为2.5.这是一个小例子.我的行向量可能很大.我是MatLab的新手,所以有没有一种有效的方法可以使用MATLAB的有效矩阵/数组操作很好地做到这一点?

Hope title isn't confusing. It's simple to show by example. I have a row vector like so: [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 row vectors might be very large. I'm new to MatLab so is there some efficient way of using MATLAB's efficient matrix/array manipulation to do this nicely?

SOF上已经有一个类似的问题,但这是专门针对MATLAB的问题!

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

谢谢:)

@gnovice询问,我想要绝对的区别.

As queried by @gnovice, I wanted the absolute difference.

推荐答案

使用 diff mean

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

示例

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

这可行,但是@Jonas的答案是正确的解决方案.

This works but @Jonas' answer is the correct solution.

编辑

Edit

来自@ gnovice,@ vivid-colours和@sevenless评论.

From @gnovice, @vivid-colours and @sevenless comments.

差的绝对值的平均值可以通过在(1)中插入 abs 来找到

The mean of the absolute value of the difference can be found by inserting abs into (1)

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

这篇关于查找数组中元素之间平均差的有效方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-28 05:20