comparison sparse2cell.m @ 200:ef41fde95ac4 feature/beams

Merged feature/grids into feature/beams.
author Jonatan Werpers <jonatan@werpers.com>
date Mon, 13 Jun 2016 16:59:02 +0200
parents d90f540f4137
children
comparison
equal deleted inserted replaced
181:419ec303e97d 200:ef41fde95ac4
1 % sparse2cell breaks a sparse matrix up into a cell matrix of sparse matrices.
2 % any zero submatrix creates a empty cell in the cell matrix.
3 % A -- NxM sparse matrix
4 % d1, d2 -- vectors of sub matrix sizes for each dimensions. Must have sum(di) == Ni.
5 % Example:
6 % C = sparse2cell(A,[5 10], [10 5])
7 function C = sparse2cell(A, d1, d2)
8 [n, m] = size(A);
9 if n ~= sum(d1) || m ~= sum(d2)
10 error('sparse2cell:NonMatchingDim','The elements of d1 and d2 must sum to N and M.');
11 end
12
13 C = cell(length(d1), length(d2));
14 I = 1;
15 for i = 1:length(d1)
16 J = 1;
17 for j = 1:length(d2)
18 Asub = A(I:(I + d1(i)-1), J:(J + d2(j)-1));
19 if nnz(Asub) == 0
20 C{i,j} = [];
21 else
22 C{i,j} = Asub;
23 end
24 J = J + d2(j);
25 end
26 I = I + d1(i);
27 end
28 end