Mercurial > repos > public > sbplib
annotate cell2sparse.m @ 163:51aaf67a7df5 feature/grids
Fixed naming and added functions for converting vectors to matrices for structured grid.
| author | Jonatan Werpers <jonatan@werpers.com> |
|---|---|
| date | Mon, 22 Feb 2016 13:55:19 +0100 |
| parents | a5fa3c2d0aa3 |
| children |
| rev | line source |
|---|---|
| 0 | 1 function A = cell2sparse(C) |
|
73
a5fa3c2d0aa3
Fixed problem with cell2sparse for empty matrices.
Jonatan Werpers <jonatan@werpers.com>
parents:
0
diff
changeset
|
2 |
|
a5fa3c2d0aa3
Fixed problem with cell2sparse for empty matrices.
Jonatan Werpers <jonatan@werpers.com>
parents:
0
diff
changeset
|
3 if isempty(C) |
|
a5fa3c2d0aa3
Fixed problem with cell2sparse for empty matrices.
Jonatan Werpers <jonatan@werpers.com>
parents:
0
diff
changeset
|
4 A = sparse([]); |
|
a5fa3c2d0aa3
Fixed problem with cell2sparse for empty matrices.
Jonatan Werpers <jonatan@werpers.com>
parents:
0
diff
changeset
|
5 return |
|
a5fa3c2d0aa3
Fixed problem with cell2sparse for empty matrices.
Jonatan Werpers <jonatan@werpers.com>
parents:
0
diff
changeset
|
6 end |
|
a5fa3c2d0aa3
Fixed problem with cell2sparse for empty matrices.
Jonatan Werpers <jonatan@werpers.com>
parents:
0
diff
changeset
|
7 |
| 0 | 8 n = row_height(C); |
| 9 m = col_width(C); | |
| 10 | |
| 11 N = sum(n); | |
| 12 M = sum(m); | |
| 13 | |
| 14 A = sparse(N,M); | |
| 15 | |
| 16 n_ind = [0 cumsum(n)]; | |
| 17 m_ind = [0 cumsum(m)]; | |
| 18 | |
| 19 for i = 1:size(C,1) | |
| 20 for j = 1:size(C,2) | |
| 21 if ~has_matrix(C{i,j}) | |
| 22 continue | |
| 23 end | |
| 24 A(n_ind(i)+1:n_ind(i+1),m_ind(j)+1:m_ind(j+1)) = C{i,j}; | |
| 25 end | |
| 26 end | |
| 27 | |
| 28 end | |
| 29 | |
| 30 function m = col_width(C) | |
| 31 for j = 1:size(C,2) | |
| 32 for i = 1:size(C,1) | |
| 33 if ~has_matrix(C{i,j}) | |
| 34 continue | |
| 35 end | |
| 36 m(j) = size(C{i,j},2); | |
| 37 end | |
| 38 end | |
| 39 end | |
| 40 | |
| 41 function n = row_height(C) | |
| 42 for i = 1:size(C,1) | |
| 43 for j = 1:size(C,2) | |
| 44 if ~has_matrix(C{i,j}) | |
| 45 continue | |
| 46 end | |
| 47 n(i) = size(C{i,j},1); | |
| 48 end | |
| 49 end | |
| 50 end | |
| 51 | |
| 52 function b = has_matrix(c) | |
| 53 b = ~(isempty(c) || (numel(c)==1 && c == 0)); | |
| 54 end |
