File: RandLim.m

package info (click to toggle)
psychtoolbox-3 3.0.18.12.dfsg1-1.1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 83,576 kB
  • sloc: ansic: 173,181; cpp: 20,885; objc: 5,148; sh: 2,752; python: 1,366; php: 384; makefile: 193; java: 113
file content (25 lines) | stat: -rw-r--r-- 885 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
function out = RandLim(n,lower,upper)
% returns pseudo-random values drawn from a uniform distribution with lower
% and upper limits LOWER and UPPER.
% LOWER and UPPER can also be matrices of the same shape as rand(N) would
% create
%
% N is a vector indicating the dimensions of the output:
%   N = [3]      creates a    3x3 matrix
%   N = [1,3]    creates a    1x3 matrix
%   N = [17,1,3] creates a 17x1x3 matrix
%
% LOWER and UPPER can be any number

% DN 2008-07-21 Wrote it
% DN 2008-09-19 Support for vector lower and upper limits
% DN 2016-08-25 Switched from deprecated nargchk to narginchk

narginchk(3,3);

r = rand(n);
if (~isscalar(lower) && any(size(lower)~=size(r))) || (~isscalar(upper) && any(size(upper)~=size(r)))
    error('LOWER and UPPER must each be scalar or have the same shape as rand(N)')
else
    out = lower + r.*(upper-lower);
end