-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsparse2alist.m
65 lines (57 loc) · 1.69 KB
/
sparse2alist.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
64
65
function sparse2alist(Q, H, file_name)
[M, N] = size(H);
file = fopen(file_name, 'w');
if Q==2
fprintf(file, '%d %d\n', N, M);
else
fprintf(file, '%d %d %d\n', N, M, Q);
end
column_weights = sum(full((H>0)));
row_weights = sum(full((H>0)'));
c_max = max(column_weights);
r_max = max(row_weights);
fprintf(file, '%d %d\n', c_max, r_max);
fprintf(file, '%d ', column_weights(1:end-1));
fprintf(file, '%d\n', column_weights(end));
fprintf(file, '%d ', row_weights(1:end-1));
fprintf(file, '%d\n', row_weights(end));
for i = 1:N
indexes = (find(H(:,i)))';
values = (full(H(indexes, i)))';
if Q == 2
temp = indexes;
else
temp = [indexes; values];
end
fprintf(file, '%d ', temp);
if length(indexes) < c_max
if Q == 2
fillers = zeros(1, c_max - length(indexes));
else
fillers = zeros(1, 2*(c_max - length(indexes)));
end
fprintf(file, '%d ', fillers);
end
fprintf(file, '\n');
end
for i = 1:M
indexes = find(H(i,:));
values = full(H(i, indexes));
if Q == 2
temp = indexes;
else
temp = [indexes; values];
end
fprintf(file, '%d ', temp);
if length(indexes) < r_max
if Q == 2
fillers = zeros(1, r_max - length(indexes));
else
fillers = zeros(1, 2*(r_max - length(indexes)));
end
fprintf(file, '%d ', fillers);
end
fprintf(file, '\n');
end
fclose(file);
end