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