检测任何值是否在彼此的某个值之内-MATLAB [英] Detecting if any values are within a certain value of each other - MATLAB

查看:89
本文介绍了检测任何值是否在彼此的某个值之内-MATLAB的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有

a = [5000 8000 20000 22000 30000]';

有没有一种快速的方法来检测给定的数字是否在另一个数字之内(例如5000),然后将其删除并用中点替换?理想情况下,我想将此附加到IF语句上,但我真的不知道可以使用哪种函数或数学方法来执行此操作.

Is there any quick way to detect if a given number is within - for example 5000 - of another and then remove both and replace them with the midpoint? Ideally I want to attach this to an IF statement but I really have no idea what sort of function or mathematical method could be used to do this.

所需的输出将是a:

   = [6500 21000 30000];

推荐答案

的方法可能最适合解决它-

accumarray based approach might be best suited to solve it -

th = 5000      %// threshold to choose next elements (edit to specific input)
a = sort(a(:)) %// sort elements
matches = diff(a)>th
idx = cumsum([0 ; matches])+1 %// index each element with their group IDs
out = (accumarray(idx,a,[],@min) + accumarray(idx,a,[],@max))./2
   %// outputs are the midpoints of the min-max boundaries within each group

这篇关于检测任何值是否在彼此的某个值之内-MATLAB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆