~ubuntu-branches/ubuntu/trusty/psychtoolbox-3/trusty-proposed

« back to all changes in this revision

Viewing changes to Psychtoolbox/PsychProbability/URandSel.m

  • Committer: Package Import Robot
  • Author(s): Yaroslav Halchenko
  • Date: 2013-11-19 23:34:50 UTC
  • mfrom: (3.1.4 experimental)
  • Revision ID: package-import@ubuntu.com-20131119233450-f7nf92vb8qavjmk8
Tags: 3.0.11.20131017.dfsg1-3
Upload to unsable since fresh glew has arrived to sid!

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
function sel = URandSel(in,n)
2
2
% sel = RandSel(in,n)
3
 
%   randomly selects N elements from set IN, each element from IN will
4
 
%   be selected up to one time (so sampling without replacing).
5
 
%   IN can have any number of dimensions and elements
 
3
%   if N is a scalar
 
4
%     randomly selects N elements from set IN, each element from IN will
 
5
%     be selected up to one time (so sampling without replacing).
 
6
%     Output is of size 1xN
 
7
%   if N is a vector
 
8
%     randomly selects prod(N) elements from set IN, without replacing. N
 
9
%     specifies the shape of the output, e.g., if N=[10 10 5], SEL will be
 
10
%     a 10x10x5 matrix
 
11
%   IN can be of any datatype and can have any number of dimensions and
 
12
%   elements
6
13
 
7
14
% 2008-08-27 DN Wrote it
8
 
 
9
 
psychassert(n<=numel(in),'More return elements requested (n: %d) than number of elements in input (n: %d)',n,numel(in));
10
 
 
11
 
selind  = NRandPerm(numel(in),n);
12
 
sel     = in(selind);
 
15
% 2012-06-13 DN Can now also return shaped inputs through non-scalar n input
 
16
 
 
17
if isscalar(n)
 
18
    nElem = n;
 
19
    n     = [1,n];
 
20
else
 
21
    nElem = prod(n);
 
22
end
 
23
 
 
24
psychassert(nElem<=numel(in),'More return elements requested (prod(n): %d) than number of elements in input (numel(in): %d)',nElem,numel(in));
 
25
 
 
26
selind  = NRandPerm(numel(in),nElem);
 
27
sel     = reshape(in(selind),n);