Mercurial > matrix-functions
comparison matrixcomp/sparsify.m @ 0:8f23314345f4 draft
Create local repository for matrix toolboxes. Step #0 done.
author | Antonio Pino Robles <data.script93@gmail.com> |
---|---|
date | Wed, 06 May 2015 14:56:53 +0200 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:8f23314345f4 |
---|---|
1 function A = sparsify(A, p) | |
2 %SPARSIFY Randomly set matrix elements to zero. | |
3 % S = SPARSIFY(A, P) is A with elements randomly set to zero | |
4 % (S = S' if A is square and A = A', i.e. symmetry is preserved). | |
5 % Each element has probability P of being zeroed. | |
6 % Thus on average 100*P percent of the elements of A will be zeroed. | |
7 % Default: P = 0.25. | |
8 | |
9 if nargin < 2, p = 0.25; end | |
10 if p<0 | p>1, error('Second parameter must be between 0 and 1 inclusive.'), end | |
11 | |
12 [m,n] = size(A); | |
13 | |
14 if ~isequal(A,A') | |
15 A = A .* (rand(m,n) > p); % Unsymmetric case | |
16 else | |
17 A = triu(A,1) .* (rand(m,n) > p); % Preserve symmetry | |
18 A = A + A'; | |
19 A = A + diag( diag(A) .* (rand(size(n,1)) > p) ); | |
20 end |