Borrar filtros
Borrar filtros

Using accumarray to create histograms from random integers

9 visualizaciones (últimos 30 días)
Hi,
I'd like to create N histograms, each from a dataset of n_vals random integers from 1 to h_length. I can demonstrate my goal for N=1 using accumarray:
N=1;
h_length=10000;
n_vals=900;
%create n_vals random integers between one and h_length
inds=floor((h_length)*rand(n_vals,N)+1);
rand_hists=accumarray(inds,1,[h_length,1]);
%this plot demonstrates the output.
figure;plot(rand_hists)
However, I'm not sure how to do this for an N larger than 1. Ultimately, I want N~=2000. I'm pretty sure I will have a memory limitation and have to do some looping, but I would like to vectorize and do it as efficiently as possible.
Any advice is greatly appreciated.
Thanks, Luke
Comment on this

Respuesta aceptada

Fangjun Jiang
Fangjun Jiang el 18 de Oct. de 2011
Check to see if you have these function.
randi()
hist()
  1 comentario
Luke S
Luke S el 18 de Oct. de 2011
I have both! randi() I didn't know about before, and it seems to save a little bit of time over floor(randn()).
Using histc() can operate on an Nxh_length array of inds as I wanted, but it actually seems o be ending up slower than looping over accumarray N times.

Iniciar sesión para comentar.

Más respuestas (1)

Peter Perkins
Peter Perkins el 19 de Oct. de 2011
Luke, I'm not exactly sure what you're asking, but as I interpret it, you want a single call to accumarray to create N sets of counts. You may find that this is no faster than a loop around accumarray, but the following code is what I think you're looking for. It passes accumarray 2-D indices (which can be done in two different ways, here I used separate column vectors), where the second index is just an "indicator variable that says which "set" of counts a given row should be put in.
I've replaced your call to rand with randi, and I'm not sure what you're plot was supposed to be doing, but I more or less kept it as is.
N=3;
h_length=10000;
n_vals=900;
%create n_vals random integers between one and h_length
iinds = randi(h_length,n_vals,N);
jinds = repmat(1:N,n_vals,1);
rand_hists = accumarray({iinds(:) jinds(:)},1,[h_length,N]);
%this plot demonstrates the output.
for i = 1:N
subplot(N,1,i), plot(rand_hists(:,i),'.')
end

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by