Binning elements between limits
조회 수: 1 (최근 30일)
이전 댓글 표시
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?
댓글 수: 0
답변 (3개)
Oleg Komarov
2012년 8월 6일
편집: Oleg Komarov
2012년 8월 6일
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
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
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 Center 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!