简体   繁体   English

在matlab中分箱

[英]Binning in matlab

I have been unable to find a function in matlab or octave to do what I want. 我一直无法找到matlab或octave中的函数来做我想做的事情。 I have a matrix m of two columns (x and y values). 我有一个两列的矩阵m(x和y值)。 I know that I can extract the column by doing m(:,1) or m(:,2). 我知道我可以通过m(:,1)或m(:,2)来提取列。 I want to split it into smaller matricies of [potentially] equal size and and plot the mean of these matricies. 我想把它分成[可能]大小相等的较小的基质,并绘制这些基质的平均值。 In other words, I want to put the values into bins based on the x values, then find means of the bins. 换句话说,我想根据x值将值放入bin中,然后找到bin的方法。 I feel like the hist function should help me, but it doesn't seem to. 我觉得hist函数应该对我有帮助,但似乎没有。

Does anyone know of a built-in function to do something like this? 有没有人知道内置函数做这样的事情?

edit 编辑

I had intended to mention that I looked at hist and couldn't get it to do what I wanted, but it must have slipped my mind. 我本来打算提一下,我看了一下hist,无法让它做我想做的事,但它一定是我的想法。

Example: Let's say I have the following (I'm trying this in octave, but afaik it works in matlab): 示例:假设我有以下内容(我在八度音程中尝试此操作,但afaik在matlab中有效):

x=1:20;
y=[1:10,10:1];
m=[x, y];

If I want 10 bins, I would like m to be split into: 如果我想要10个箱子,我希望将其拆分为:

m1=[1:2, 1:2]
...
m5=[9:10, 9:10]
m6=[10:11, 10:-1:9]
...
m10=[19:20, 2:-1:1]

and then get the mean of each bin. 然后得到每个bin的平均值。

Update: I have posted a follow-up question here . 更新:我在这里发布了一个后续问题。 I would greatly appreciate responses. 我非常感谢回复。

I have answered this in video form on my blog: 我在博客上以视频形式回答了这个问题:

http://blogs.mathworks.com/videos/2009/01/07/binning-data-in-matlab/ http://blogs.mathworks.com/videos/2009/01/07/binning-data-in-matlab/

Here is the code: 这是代码:

m = rand(10,2); %Generate data

x = m(:,1); %split into x and y
y = m(:,2);

topEdge = 1; % define limits
botEdge = 0; % define limits
numBins = 2; % define number of bins

binEdges = linspace(botEdge, topEdge, numBins+1);

[h,whichBin] = histc(x, binEdges);

for i = 1:numBins
    flagBinMembers = (whichBin == i);
    binMembers     = y(flagBinMembers);
    binMean(i)     = mean(binMembers);
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM