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 |
5720
|
14 ## along with Octave; see the file COPYING. If not, write to the Free |
|
15 ## Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
16 ## 02110-1301, USA. |
5551
|
17 |
|
18 ## -*- texinfo -*- |
5657
|
19 ## @deftypefn {Function File} {} blkdiag (@var{a}, @var{b}, @var{c}, @dots{}) |
|
20 ## Build a block diagonal matrix from @var{a}, @var{b}, @var{c}, @dots{}. |
5551
|
21 ## All the arguments must be numeric and are two-dimensional matrices or |
|
22 ## scalars. |
5642
|
23 ## @seealso{diag, horzcat, vertcat} |
5551
|
24 ## @end deftypefn |
|
25 |
|
26 ## Author: Daniel Calvelo |
|
27 ## Modified by: William Poetra Yoga Hadisoeseno |
|
28 |
|
29 function retval = blkdiag (varargin) |
|
30 |
|
31 if (nargin < 1) |
6046
|
32 print_usage (); |
5551
|
33 endif |
|
34 |
5989
|
35 if (! all (cellfun (@isnumeric, varargin))) |
5552
|
36 error ("blkdiag: all arguments must be numeric"); |
5551
|
37 endif |
|
38 |
5552
|
39 ## Note: trailing singletons are automatically (correctly) ignored. |
5569
|
40 if (! all (cellfun ("ndims", varargin) == 2)) |
5657
|
41 error ("blkdiag: all arguments must be two-dimensional matrices"); |
5551
|
42 endif |
|
43 |
5552
|
44 ## size is an option for cellfun, but it's a bit different from |
|
45 ## calling size directly. |
5989
|
46 tmp = cell2mat (cellfun (@size, varargin', "UniformOutput", false)); |
|
47 csz = cumsum ([0 0; tmp], 1); |
5551
|
48 retval = zeros (csz(end,:)); |
5592
|
49 |
|
50 for p = 1:nargin |
5551
|
51 retval((csz(p,1)+1):csz(p+1,1),(csz(p,2)+1):csz(p+1,2)) = varargin{p}; |
|
52 endfor |
|
53 |
|
54 endfunction |
|
55 |
|
56 # regular tests |
|
57 %!assert(blkdiag(1,ones(2),1),[1,0,0,0;0,1,1,0;0,1,1,0;0,0,0,1]) |
|
58 %!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]) |
|
59 %!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]) |
|
60 %!assert(blkdiag([1,2;3,4],[5,6,7]),[1,2,0,0,0;3,4,0,0,0;0,0,5,6,7]) |
|
61 # tests involving empty matrices |
|
62 %!assert(blkdiag([],[],[]),[]) |
|
63 %!assert(blkdiag([],[1,2;3,4],[],5,[]),[1,2,0;3,4,0;0,0,5]) |
5601
|
64 %!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
|
65 # sanity checks |
|
66 %!test |
|
67 %! A = rand (round (rand (1, 2) * 10)); |
|
68 %! assert (blkdiag (A), A); |