-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsp_rand.m
executable file
·58 lines (50 loc) · 1.91 KB
/
sp_rand.m
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
function r = sp_rand(a, b, varargin)
% function r = sp_rand(a, b, varargin)
%
% Returns uniformly distributed pseudorandom numbers from a certain range
%
% r = sp_rand(a,b,N) returns [N x N] matrix with numbers from the range [a b]
% r = sp_rand(a,b,M,N) returns [M x N] matrix with numbers from the range [a b]
% r = sp_rand(a,b,[M N]) returns [M x N] matrix with numbers from the range [a b]
%
% INPUTS:
% a : [double] smallest value for the range
% b : [double] biggest value for the range
% See above for more arguments
%
% OUTPUTS:
% r : [matrix]
%
% Sagi Perel, 06/2009
switch(nargin)
case 3
if(~isvector(varargin{1}))
error('sp_rand: input arguments must be vectors (and non empty)');
end
if(size(varargin{1},1) == 1)
switch(size(varargin{1},2))
case 1
output_size = [varargin{1} varargin{1}];
case 2
output_size = varargin{1};
otherwise
error('sp_rand: third input arguments must be sized either [1 1] or [1 2]');
end
else
error('sp_rand: third input arguments must be sized either [1 1] or [1 2]');
end
case 4
if(~isvector(varargin{1}) || ~isvector(varargin{2}))
error('sp_rand: input arguments must be vectors (and non empty)');
end
if(~isscalar(varargin{1}) || ~isscalar(varargin{2}))
error('sp_rand: input arguments must be scalars');
end
output_size = [varargin{1} varargin{2}];
otherwise
error('sp_rand: wrong number of input arguments supplied');
end
if(~isscalar(a) || ~isscalar(b))
error('sp_rand: a and b must be scalars');
end
r = a + (b-a).*rand(output_size);