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)
  댓글 수: 1
NS
NS 2012년 8월 7일
Thanks Oleg. I will have a look at it.

댓글을 달려면 로그인하십시오.


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
  댓글 수: 1
NS
NS 2012년 8월 7일
Thanks John. I will have a look at it.

댓글을 달려면 로그인하십시오.


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
  댓글 수: 1
NS
NS 2012년 8월 7일
Thanks Matt. I will have a look at it.

댓글을 달려면 로그인하십시오.

카테고리

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