How to retrieve the indices of the values in each bin?

27 views (last 30 days)
Lu Da Silva
Lu Da Silva on 10 Oct 2021
Commented: Walter Roberson on 28 Oct 2021
I have a histogram with values from a vector A , spread into 30 bins. How can I get the indices of the values in A, that correspond to each bin?
Example:
A = [ 4 6 8 2 5 3 3]
bin number 4 contains [4 3 3]
so I want to have a vecor B containing the indices B = [1 6 7]

Answers (1)

Walter Roberson
Walter Roberson on 10 Oct 2021
The code could be slightly simpler if all of the bins were only one value wide.
A = randi(60, 1, 50)
A = 1×50
36 10 40 43 35 52 52 43 21 44 23 47 8 31 46 19 59 56 47 52 46 47 58 59 25 48 31 59 53 13
edges = [1:2:60, inf]
edges = 1×31
1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49 51 53 55 57 59
[counts, ~, bins] = histcounts(A, edges)
counts = 1×30
1 1 0 2 1 1 1 1 0 2 2 1 3 1 1 3 0 2 0 1 1 3 2 4 1 4 2 2 4 3
bins = 1×50
18 5 20 22 18 26 26 22 11 22 12 24 4 16 23 10 30 28 24 26 23 24 29 30 13 24 16 30 27 7
B = accumarray(bins(:), reshape(1:numel(bins), [], 1), [], @(V){V.'})
B = 30×1 cell array
{[ 48]} {[ 37]} {0×0 double} {[ 13 45]} {[ 2]} {[ 33]} {[ 30]} {[ 32]} {0×0 double} {[ 16 46]} {[ 9 50]} {[ 11]} {[25 31 43]} {[ 42]} {[ 39]} {[14 27 35]}
  3 Comments
Walter Roberson
Walter Roberson on 28 Oct 2021
Note that in the following code, any value in A that is outside the range of the edges will not have its index appear anywhere in B.
A = randi([-2 60], 1, 50)
A = 1×50
58 21 4 42 56 -2 4 20 34 29 40 26 58 4 60 -2 29 6 -2 44 38 28 32 33 28 52 9 1 8 33
edges = [1:2:60, inf]
edges = 1×31
1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49 51 53 55 57 59
[counts, ~, bins] = histcounts(A, edges)
counts = 1×30
2 5 2 2 1 1 2 1 0 1 1 0 1 2 3 2 5 0 2 1 1 1 2 1 1 1 0 2 2 1
bins = 1×50
29 11 2 21 28 0 2 10 17 15 20 13 29 2 30 0 15 3 0 22 19 14 16 17 14 26 5 1 4 17
valididx = reshape(find(bins), [], 1);
B = accumarray( reshape(bins(valididx), [], 1), valididx, [], @(V){V.'})
B = 30×1 cell array
{[ 28 39]} {[3 7 14 31 37]} {[ 18 38]} {[ 29 47]} {[ 27]} {[ 46]} {[ 34 50]} {[ 42]} {0×0 double } {[ 8]} {[ 2]} {0×0 double } {[ 12]} {[ 22 25]} {[ 10 17 44]} {[ 23 40]}

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!