如何在MATLAB中翻转histc函数的edges(1)和edges(end)的定义?

发布于 2024-08-08 00:42:51 字数 305 浏览 5 评论 0 原文

在 MATLAB: 中

n = histc(x,edges);

定义如下:

n(k) 对值 x(i) 进行计数,如果有edges(k) <= x(i) <=边(k+1)。最后一个垃圾箱 计算任何匹配的 x 值 边缘(结束)。

有没有什么方法可以翻转结束行为,使得 n(1) 计算与edges(1)匹配的x的任何值,并且n(end)计算满足edges(end-1) <=的值x(i) x(i) <边缘(结束)?

In MATLAB:

n = histc(x,edges);

is defined to behave as follows:

n(k) counts the value x(i) if edges(k)
<= x(i) < edges(k+1). The last bin
counts any values of x that match
edges(end).

Is there any way to flip the end behavior such that n(1) counts any values of x that match edges(1), and n(end) counts the values x(i) that satisfy edges(end-1) <= x(i) < edges(end)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

旧情别恋 2024-08-15 00:42:51

考虑以下代码:

n = histc(x, [edges(1) edges]);
n(1) = sum(x==edges(1));
n(end) = [];

根据发布的问题,上面将返回:

  • n(1):计算与边(1)匹配的 x 的任何值
  • n(k) [k~=1]:如果 Edges(k-1) <= x(i) edges(k-1) <= x(i) ,则对值 x(i) 进行计数Edges(k)

这与 gnovice 解决方案,因为他的答案使用了边界:edges(k-1) edges(k-1) edges(k-1) edges(k-1) edges(k-1) edges(k-1) x(i) <= Edges(k) (注意等号的位置)。


为了进行演示,请考虑这个简单的示例:

x = [0 1 1.5 2 2.5 4 6.5 8 10];
edges = 0:2:10;

>> n = fliplr(histc(-x,-fliplr(edges)))
n =
     1     3     2     0     2     1

对应的区间:
0 (0,2] (2,4] (4,6] (6,8] (8,10]

反对:

>> n = histc(x, [edges(1) edges]);
>> n(1) = sum(x==edges(1));
>> n(end) = []
n =
     1     3     2     1     1     1

对应的区间:
0 [0,2) [2,4) [4,6) [6,8) [8,10)

Consider the following code:

n = histc(x, [edges(1) edges]);
n(1) = sum(x==edges(1));
n(end) = [];

According to the question posted, the above will return:

  • n(1): counts any values of x that match edges(1)
  • n(k) [k~=1]: counts the value x(i) if edges(k-1) <= x(i) < edges(k)

This different from gnovice solution in that his answer uses the bounds: edges(k-1) < x(i) <= edges(k) (note the position of the equality sign).


To demonstrate, consider this simple example:

x = [0 1 1.5 2 2.5 4 6.5 8 10];
edges = 0:2:10;

>> n = fliplr(histc(-x,-fliplr(edges)))
n =
     1     3     2     0     2     1

corresponding to the intervals:
0 (0,2] (2,4] (4,6] (6,8] (8,10]

Against:

>> n = histc(x, [edges(1) edges]);
>> n(1) = sum(x==edges(1));
>> n(end) = []
n =
     1     3     2     1     1     1

corresponding to the intervals:
0 [0,2) [2,4) [4,6) [6,8) [8,10)

小忆控 2024-08-15 00:42:51

由于edges参数必须具有单调非递减的值,因此翻转边缘行为的一种方法是否定并翻转edges参数并否定分箱的值。如果您随后翻转 HISTC 的 bin 计数输出,您应该看到 HISTC 的典型边缘行为相反:

n = fliplr(histc(-x,-fliplr(edges)));

上面使用 FLIPLR,因此xedges应该是行向量(即1×N)。此代码将根据以下条件对数据进行分箱:

  • 第一个分箱 n(1) 对与 edges(1) 匹配的 x 值进行计数。
  • 其他箱 n(k) 对值 x(i) 进行计数,使得 edges(k-1) edges(k-1) x(i) <= 边(k)

请注意,这会翻转所有 bin 的边缘行为,而不仅仅是第一个和最后一个 bin! bin 典型行为>n(k) 使用方程edges(k) <= x(i) edges(k) <= x(i) Edges(k+1) (注意索引之间的差异以及哪一侧有等号!)。


编辑:经过一番讨论...

如果您相反想根据以下标准对数据进行分箱:

  • 第一个分箱n(1)计数与 edges(1) 匹配的任何 x 值。
  • 第二个 bin n(2) 对值 x(i) 进行计数,使得 edges(1) x(i) <边缘(2)
  • 其他箱n(k)对值x(i)进行计数,使得edges(k-1) <= x(i) <边(k)。

然后,以下内容应该完成此操作:

n = histc(x,[edges(1) edges(1)+eps(edges(1)) edges(2:end)]);
n(end) = [];

第一个 bin 应仅捕获等于 edges(1) 的值,而第二个 bin 的下边缘应从高于 edges(1) 的增量值开始 (使用 EPS 函数找到)。最后一个 bin,它计算等于 edges(end) 的值的数量,被丢弃。

Since the edges argument has to have monotonically nondecreasing values, one way to flip the edge behavior is to negate and flip the edges argument and negate the values for binning. If you then flip the bin count output from HISTC, you should see the typical edge behavior of HISTC reversed:

n = fliplr(histc(-x,-fliplr(edges)));

The above uses FLIPLR, so x and edges should be row vectors (i.e. 1-by-N). This code will bin data according to the following criteria:

  • The first bin n(1) counts any values of x that match edges(1).
  • The other bins n(k) count the values x(i) such that edges(k-1) < x(i) <= edges(k).

Note that this flips the edge behavior of all the bins, not just the first and last bins! The typical behavior of HISTC for bin n(k) uses the equation edges(k) <= x(i) < edges(k+1) (Note the difference between the indices and which side has the equals sign!).


EDIT: After some discussion...

If you instead wanted to bin data according to the following criteria:

  • The first bin n(1) counts any values of x that match edges(1).
  • The second bin n(2) counts the values x(i) such that edges(1) < x(i) < edges(2).
  • The other bins n(k) count the values x(i) such that edges(k-1) <= x(i) < edges(k).

Then the following should accomplish this:

n = histc(x,[edges(1) edges(1)+eps(edges(1)) edges(2:end)]);
n(end) = [];

The first bin should capture only values equal to edges(1), while the lower edge of the second bin should start at an incremental value above edges(1) (found using the EPS function). The last bin, which counts the number of values equal to edges(end), is thrown out.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文