How to retrieve the indices of the values in each bin?
37 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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]
0 comentarios
Respuestas (1)
Walter Roberson
el 10 de Oct. de 2021
The code could be slightly simpler if all of the bins were only one value wide.
A = randi(60, 1, 50)
edges = [1:2:60, inf]
[counts, ~, bins] = histcounts(A, edges)
B = accumarray(bins(:), reshape(1:numel(bins), [], 1), [], @(V){V.'})
3 comentarios
Walter Roberson
el 28 de Oct. de 2021
If accumarray() is giving you that error, then it implies that some value in your matrix A is less than the first value in your edges vector or greater than the last one. For example,
A = randi([-2 60], 1, 50)
edges = [1:2:60, inf]
[counts, ~, bins] = histcounts(A, edges)
B = accumarray(bins(:), reshape(1:numel(bins), [], 1), [], @(V){V.'})
See how the 5th bin number is 0, which corresponds to the bin for the input value 0 in A, and 0 is before the first value in edges . (It is not because A has negative or 0 entries: it is strictly to do with the fact that it has entries that are outside the range of the edges list.)
Walter Roberson
el 28 de Oct. de 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)
edges = [1:2:60, inf]
[counts, ~, bins] = histcounts(A, edges)
valididx = reshape(find(bins), [], 1);
B = accumarray( reshape(bins(valididx), [], 1), valididx, [], @(V){V.'})
Ver también
Categorías
Más información sobre Data Distribution Plots en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!