Using accumarray to create histograms from random integers
5 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
更多回答(1 个)
Peter Perkins
2011-10-19
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 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Hypothesis Tests 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!