Can histogram bin edges and width be specified for the same histogram?

I'd like to bin data for analysis, and then repeat the analysis with differing time windows/durations. In this case, I'm analyzing neuronal spike times after binning the data like this:
edges=0:0.5:116.5;
binnedspks=histogram(sortedspikes,edges)
But there doesn't seem to be a way to specify that the data be binned in thinner bins after edges are specified, or vice versa, which would require each successive bin to skip over some of the data.
I've tried a few combinations of things like this but the edges input just seems to overwrite the width:
histogram(sortedspikes,'BinWidth',0.5,'BinEdges',edges)

 Respuesta aceptada

The edges input is not required to be a uniformly spaced vector.
x = rand(1, 1e5);
E = [0 2.^(-5:0)];
h = histogram(x, E, 'Normalization', 'probability');
xticks(E)
xticklabels(["0", "2^{" + string(-5:0) + "}"])
You can see that about half the numbers are in the bin from 1/2 to 1, about a quarter in the bin from 1/4 to 1/2, etc. You can also see that the BinEdges are not uniformly spaced, and the histogram will tell you that if you ask for the width of the bins.
h.BinEdges
ans = 1×7
0 0.0312 0.0625 0.1250 0.2500 0.5000 1.0000
h.BinWidth
ans = 'nonuniform'

4 comentarios

Thank you for your answer, but I wasn't specific enough in my question. Sorry. I want to bin the data in uniform, narrower bins, but keep the same left edges of the bins, to allow the bins to skip over some data.
So if your data spans the range [0, 1] you'd like to have bins [0, 0.25) and [0.5, 0.75) without having bins [0.25, 0.5) and [0.75, 1] be included in the histogram?
x = rand(1, 1e5);
E = 0:0.25:1;
figure;
h = histogram(x, E);
figure;
h = histogram(x, E);
h.BinCounts(2:2:end) = 0;
Yes, that's correct!
I guess I hadn't thought of removing data after the fact, but that seems feasible. I'll give it a try and see if it will work for my purposes. Thank you for devoting some time to this.

Iniciar sesión para comentar.

Más respuestas (1)

You can specify the bin widths
for k = 1 : 20
binWidth = k / 10; % Whatever...
edges = 0 : binWidth : 116.5;
counts = histcounts(sortedspikes, edges)
bar(edges, counts);
xlabel('Value');
ylabel('Count');
grid on;
drawnow;
pause(1); % Wait a short time so you can see the histogram.
end

1 comentario

Thank you for your answer, but I wasn't specific enough in my question. Sorry. I want to bin the data in uniform, narrower bins, but keep the same left edges of the bins, to allow the bins to skip over some data.

Iniciar sesión para comentar.

Categorías

Más información sobre Data Distribution Plots en Centro de ayuda y File Exchange.

Productos

Versión

R2019a

Etiquetas

Preguntada:

el 1 de Oct. de 2021

Comentada:

el 4 de Oct. de 2021

Community Treasure Hunt

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

Start Hunting!

Translated by