Binning elements between limits

1 次查看(过去 30 天)
NS
NS 2012-8-6
Hi all,
I have a m*2 matrix. Column 1 is the X position while Column 2 is the distance. Column 2 has a range from 0-20. I want to bin the values of column 1. Bin 1 should include all the values of X that have a corresponding distance between 0.9-1.1, Bin 2 should have all values of X having distance between 1.9-2.1 and so on. I want to have about 10 bins. I want to calculate the variance of the position elements in each bin. Is there a function to do this is MATLAB?

回答(3 个)

Oleg Komarov
Oleg Komarov 2012-8-6
编辑:Oleg Komarov 2012-8-6
Yes the function is called histc(). Read the documentation carefully.
Example:
[n,bin] = histc(A(:,2), [0.9,1.1,1.9,2.1])
Then to calculate the var():
accumarray(bin, A(:,2),[],@var)

John Petersen
John Petersen 2012-8-6
编辑:John Petersen 2012-8-6
histc gives you the number of elements in the bins. Below code should give variance of the elements in the bins:
% X is position
% D is distance
binwidth = 0.1;
n = 10; % number of bins
vx = zeros(n,1);
for k=1:n
ind = (D>k-binwidth)&(D<k+bindwith); % index of bins
vx(k) = var(X(ind)); % variance of X bins
end

Matt Fig
Matt Fig 2012-8-6
Here I show an example of what I think you are getting at, though it is still a little unclear to me.
D = rand(30,2)*20; % Example data.
BINS = bsxfun(@plus,[.9 1.1].',0:18); % Example; make your own...
[I,I] = histc(D(:,2).',[-inf BINS(:).' inf]); % Pass column 2
A = accumarray(I.',D(:,1), [], @(x) {x});
A = A(2:2:end); % Here are col 1 matches to bins of col 2.
V = cellfun(@var,A); % And the variances of each

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by