English 中文(简体)
Matlab中的分组
原标题:
  • 时间:2009-01-07 04:46:36
  •  标签:

I have been unable to find a function in matlab or octave to do what I want. I have a matrix m of two columns (x and y values). I know that I can extract the column by doing m(:,1) or 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. I feel like the hist function should help me, but it doesn t seem to.

有没有任何内置函数可以做类似于这样的事情?

编辑

我原打算提到我看了看历史记录,但没能让它按照我想要的方式运行,但可能我忘了。

让我们假设我有以下内容(我在尝试在Octave中使用,但据我所知,这也适用于Matlab):

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

如果我想要10个箱子,我希望m被分成:

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

然后获取每个箱的平均值。

更新:我在这里发布了一个后续问题。真诚希望得到回应。

最佳回答

我已在我的博客上以视频形式回答了这个问题:

将此翻译成中文:http://blogs.mathworks.com/videos/2009/01/07/binning-data-in-matlab/ 在MATLAB中对数据进行分箱

这是代码:

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
问题回答

暂无回答




相关问题
热门标签