-
Notifications
You must be signed in to change notification settings - Fork 0
/
sparse_random_tensor.m
47 lines (47 loc) · 1.03 KB
/
sparse_random_tensor.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
function sparse_random_tensor(data_dir,sparse_file,I,density)
%SPARSE_RANDOM_TENSOR makes a sparse uniformly distributed random tensor
%
% Peter Turney
% October 20, 2007
%
% Copyright 2007, National Research Council of Canada
%
% assume a third-order tensor is desired
%
%% initialize
%
fprintf('SPARSE_RANDOM_TENSOR is running ...\n');
%
% make sure the directory exists
%
if (isdir(data_dir) == 0)
mkdir(data_dir);
end
%
file_name = [data_dir, '/', sparse_file];
%
fprintf(' generating tensor of size %d x %d x %d with density %f\n', ...
I(1), I(2), I(3), density);
%
%% main loop
%
file_id = fopen(file_name, 'w');
fprintf(' slice: ');
for i1 = 1:I(1)
fprintf('%d ',i1); % show progress
if ((mod(i1,10) == 0) && (i1 ~= I(1)))
fprintf('\n '); % time for new line
end
for i2 = 1:I(2)
for i3 = 1:I(3)
if (rand < density)
fprintf(file_id,'%d %d %d %f\n',i1,i2,i3,rand);
end
end
end
end
fprintf('\n');
fclose(file_id);
%
fprintf('SPARSE_RANDOM_TENSOR is done\n');
%