Mercurial > hg > octave-nkf
annotate scripts/general/blkdiag.m @ 11191:01ddaedd6ad5
Reverse changeset b1f4bdc276b6. Use all lower case for "uniformoutput" option.
author | Rik <octave@nomad.inbox5.com> |
---|---|
date | Thu, 04 Nov 2010 12:18:08 -0700 |
parents | eb63fbe60fab |
children | 994e2a93a8e2 |
rev | line source |
---|---|
8920 | 1 ## Copyright (C) 2000, 2005, 2006, 2007, 2008 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. | |
11191
01ddaedd6ad5
Reverse changeset b1f4bdc276b6. Use all lower case for "uniformoutput" option.
Rik <octave@nomad.inbox5.com>
parents:
8920
diff
changeset
|
47 tmp = cell2mat (cellfun (@size, varargin', "uniformoutput", false)); |
5989 | 48 csz = cumsum ([0 0; tmp], 1); |
5551 | 49 retval = zeros (csz(end,:)); |
5592 | 50 |
51 for p = 1:nargin | |
8120
8f0150a0d19e
fix blkdiag to not rely on Matlab-incompatible behaviour
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
52 vp = varargin{p}; |
8f0150a0d19e
fix blkdiag to not rely on Matlab-incompatible behaviour
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
53 if (! isempty (vp)) |
8f0150a0d19e
fix blkdiag to not rely on Matlab-incompatible behaviour
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
54 retval((csz(p,1)+1):csz(p+1,1),(csz(p,2)+1):csz(p+1,2)) = vp; |
8f0150a0d19e
fix blkdiag to not rely on Matlab-incompatible behaviour
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
55 endif |
5551 | 56 endfor |
57 | |
58 endfunction | |
59 | |
60 # regular tests | |
61 %!assert(blkdiag(1,ones(2),1),[1,0,0,0;0,1,1,0;0,1,1,0;0,0,0,1]) | |
62 %!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]) | |
63 %!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]) | |
64 %!assert(blkdiag([1,2;3,4],[5,6,7]),[1,2,0,0,0;3,4,0,0,0;0,0,5,6,7]) | |
65 # tests involving empty matrices | |
66 %!assert(blkdiag([],[],[]),[]) | |
67 %!assert(blkdiag([],[1,2;3,4],[],5,[]),[1,2,0;3,4,0;0,0,5]) | |
5601 | 68 %!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 | 69 # sanity checks |
70 %!test | |
71 %! A = rand (round (rand (1, 2) * 10)); | |
72 %! assert (blkdiag (A), A); |