在 Mathematica 中查找数字范围的频率 [英] Finding frequency of range of numbers in Mathematica

查看:33
本文介绍了在 Mathematica 中查找数字范围的频率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定 Mathematica 中的数字列表,我如何从该列表中提取我指定的数字 ab 之间的数字总数?

Given a list of numbers in Mathematica, how would I extract from that list the total number of numbers between numbers a and b that I specify?

推荐答案

最直接的方法就是:

Count[data, x_ /; a <= x <= b]

然而,对于大多数数据,有更快的方法,这要归功于 Carl Woll:

There are however much faster ways for most data, this one thanks to Carl Woll:

Tr@Unitize@Clip[data, {a, b}, {0, 0}]

<小时>

Carl Woll 的方法特别快,但正如 yoda 指出的那样,如果您的列表包含零,并且您的范围也跨越零,则它会失败.这是 Kevin J. McCann 的另一种处理这种情况的方法,而且速度仍然非常快:


Carl Woll's method is particularly fast, but as yoda pointed out, it fails if your list contains zeros, and your range also straddles zero. Here is another method from Kevin J. McCann that handles this case, and is still very fast:

Tr@UnitStep[(data - a)*(b - data)]

作为纯函数 [data, a, b]:

As a pure function [data, a, b]:

Tr@UnitStep[(#-#2)*(#3-#)]&

这篇关于在 Mathematica 中查找数字范围的频率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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