Using accumarray to create histograms from random integers
5 views (last 30 days)
Show older comments
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
0 Comments
Accepted Answer
More Answers (1)
Peter Perkins
on 19 Oct 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
0 Comments
See Also
Categories
Find more on Hypothesis Tests in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!