Count the appearances of each value per bin within a vector of 144 bins.

조회 수: 1 (최근 30일)
I have a vector of 21600 values between [-3;2]. I want to divide this vector in 150-value bins and count how many times each value occurs within each bin.
Ideally I would get a matrix 144*6, with columns representing the bins and rows showing the number of occurrences of the values (-3, -2, -1, 0, 1, 2) within the each bin. I tried to use histcounts and bincounts, and other similar scripts of MATLAB Community, but I wasn't successful. Thanks a lot!

채택된 답변

Guillaume
Guillaume 2018년 7월 20일
편집: Guillaume 2018년 7월 20일
A method without a loop:
%demo data:
v = randi([-3 2], 1, 21600);
binsize = 150;
assert(mod(numel(v), binsize) == 0, 'number of elements not divisible by binsize')
destrow = repelem(1:numel(v)/binsize, binsize);
result = accumarray([destrow', v' + 4], 1) %v' + 4 gives the destination column for values -3 to 2
  댓글 수: 1
Carlos Goncalves Moreira
Carlos Goncalves Moreira 2018년 7월 20일
Thank you very much, it works great! My next step would be adapting the script to any initial vector size, but your input makes that job already! Thanks again.

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

추가 답변 (1개)

Rik
Rik 2018년 7월 20일
So you want to split your original vector into 144 vectors, and then get the histcounts of each vector?
data=randi([-3 2],216000,1);
results=zeros(144,6);
for n=1:144
results(n,:)=histcounts(data(((n-1)*150+1):(n*150)),6);
end

카테고리

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