Count the appearances of each value per bin within a vector of 144 bins.
3 views (last 30 days)
Carlos Goncalves Moreira on 20 Jul 2018
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 on 20 Jul 2018
Edited: Guillaume on 20 Jul 2018
A method without a loop:
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
More Answers (1)
Rik on 20 Jul 2018
So you want to split your original vector into 144 vectors, and then get the histcounts of each vector?