forked from andrewssobral/tensor_toolbox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsptenrand.m
63 lines (53 loc) · 1.67 KB
/
sptenrand.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
59
60
61
62
63
function Y = sptenrand(sz,nz)
%SPTENRAND Sparse uniformly distributed random tensor.
%
% R = SPTENRAND(sz,density) creates a random sparse tensor of the
% specified sz with approximately density*prod(sz) nonzero
% entries.
%
% R = SPTENRAND(sz,nz) creates a random sparse tensor of the
% specified sz with approximately nz nonzero entries.
%
% Example: R = sptenrand([5 4 2],12);
%
% See also SPTENSOR, TENRAND, RAND.
%
%MATLAB Tensor Toolbox.
%Copyright 2015, Sandia Corporation.
% This is the MATLAB Tensor Toolbox by T. Kolda, B. Bader, and others.
% http://www.sandia.gov/~tgkolda/TensorToolbox.
% Copyright (2015) Sandia Corporation. Under the terms of Contract
% DE-AC04-94AL85000, there is a non-exclusive license for use of this
% work by or on behalf of the U.S. Government. Export of this data may
% require a license from the United States Government.
% The full license terms can be found in the file LICENSE.txt
% Error check on siz
if ndims(sz) ~= 2 || size(sz,1) ~= 1
error('Size must be a row vector');
end
% Error check on nz
if ~exist('nz','var') || (nz < 0)
error('2nd argument must be positive');
end
% Is nz an number or a fraction? Ultimately want a number.
if (nz < 1)
nz = prod(sz) * nz;
end
% Make sure nz is an integer
nz = ceil(nz);
% Keep iterating until we find enough unique nonzeros or we
% give up
subs = [];
cnt = 0;
while (size(subs,1) < nz) && (cnt < 10)
subs = ceil( rand(nz, size(sz,2)) * diag(sz) );
subs = unique(subs, 'rows');
cnt = cnt + 1;
end
% Extract nnz subscipts and create a corresponding list of
% values
nz = min(nz, size(subs,1));
subs = subs(1:nz,:);
vals = rand(nz,1);
Y = sptensor(subs,vals,sz);
return;