5551
|
1 ## Copyright (C) 2000 Daniel Calvelo |
|
2 ## |
|
3 ## This program is free software; you can redistribute it and/or modify |
|
4 ## it under the terms of the GNU General Public License as published by |
|
5 ## the Free Software Foundation; either version 2 of the License, or |
|
6 ## (at your option) any later version. |
|
7 ## |
|
8 ## This program is distributed in the hope that it will be useful, |
|
9 ## but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
10 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
11 ## GNU General Public License for more details. |
|
12 ## |
|
13 ## You should have received a copy of the GNU General Public License |
|
14 ## along with this program; if not, write to the Free Software |
|
15 ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|
16 |
|
17 ## -*- texinfo -*- |
5657
|
18 ## @deftypefn {Function File} {} blkdiag (@var{a}, @var{b}, @var{c}, @dots{}) |
|
19 ## Build a block diagonal matrix from @var{a}, @var{b}, @var{c}, @dots{}. |
5551
|
20 ## All the arguments must be numeric and are two-dimensional matrices or |
|
21 ## scalars. |
5642
|
22 ## @seealso{diag, horzcat, vertcat} |
5551
|
23 ## @end deftypefn |
|
24 |
|
25 ## Author: Daniel Calvelo |
|
26 ## Modified by: William Poetra Yoga Hadisoeseno |
|
27 |
|
28 function retval = blkdiag (varargin) |
|
29 |
|
30 if (nargin < 1) |
|
31 usage ("blkdiag (a, b, c, ...)"); |
|
32 endif |
|
33 |
|
34 if (! all (cell2mat (cellfun (@isnumeric, varargin)))) |
5552
|
35 error ("blkdiag: all arguments must be numeric"); |
5551
|
36 endif |
|
37 |
5552
|
38 ## Note: trailing singletons are automatically (correctly) ignored. |
5569
|
39 if (! all (cellfun ("ndims", varargin) == 2)) |
5657
|
40 error ("blkdiag: all arguments must be two-dimensional matrices"); |
5551
|
41 endif |
|
42 |
5552
|
43 ## size is an option for cellfun, but it's a bit different from |
|
44 ## calling size directly. |
5551
|
45 csz = cumsum ([0 0; (cell2mat (cellfun (@size, varargin')))], 1); |
|
46 retval = zeros (csz(end,:)); |
5592
|
47 |
|
48 for p = 1:nargin |
5551
|
49 retval((csz(p,1)+1):csz(p+1,1),(csz(p,2)+1):csz(p+1,2)) = varargin{p}; |
|
50 endfor |
|
51 |
|
52 endfunction |
|
53 |
|
54 # regular tests |
|
55 %!assert(blkdiag(1,ones(2),1),[1,0,0,0;0,1,1,0;0,1,1,0;0,0,0,1]) |
|
56 %!assert(blkdiag([1,2],[3,4],[5,6]),[1,2,0,0,0,0;0,0,3,4,0,0;0,0,0,0,5,6]) |
|
57 %!assert(blkdiag([1,2],[3;4],[5,6]),[1,2,0,0,0;0,0,3,0,0;0,0,4,0,0;0,0,0,5,6]) |
|
58 %!assert(blkdiag([1,2;3,4],[5,6,7]),[1,2,0,0,0;3,4,0,0,0;0,0,5,6,7]) |
|
59 # tests involving empty matrices |
|
60 %!assert(blkdiag([],[],[]),[]) |
|
61 %!assert(blkdiag([],[1,2;3,4],[],5,[]),[1,2,0;3,4,0;0,0,5]) |
5601
|
62 %!assert(blkdiag(zeros(1,0,1),[1,2,3],1,0,5,zeros(0,1,1)),[0,0,0,0,0,0,0;1,2,3,0,0,0,0;0,0,0,1,0,0,0;0,0,0,0,0,0,0;0,0,0,0,0,5,0]); |
5551
|
63 # sanity checks |
|
64 %!test |
|
65 %! A = rand (round (rand (1, 2) * 10)); |
|
66 %! assert (blkdiag (A), A); |