Binning elements between limits

1 vue (au cours des 30 derniers jours)
NS
NS le 6 Août 2012
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?

Réponses (3)

Oleg Komarov
Oleg Komarov le 6 Août 2012
Modifié(e) : Oleg Komarov le 6 Août 2012
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)
  1 commentaire
NS
NS le 7 Août 2012
Thanks Oleg. I will have a look at it.

Connectez-vous pour commenter.


John Petersen
John Petersen le 6 Août 2012
Modifié(e) : John Petersen le 6 Août 2012
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
  1 commentaire
NS
NS le 7 Août 2012
Thanks John. I will have a look at it.

Connectez-vous pour commenter.


Matt Fig
Matt Fig le 6 Août 2012
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
  1 commentaire
NS
NS le 7 Août 2012
Thanks Matt. I will have a look at it.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by