Mercurial > hg > octave-nkf
annotate scripts/general/blkdiag.m @ 13929:9cae456085c2
Grammarcheck of documentation before 3.6.0 release.
* accumarray.m, blkdiag.m, nargoutchk.m, nthargout.m, profexplore.m, profile.m,
computer.m, orderfields.m, recycle.m, version.m, sqp.m, matlabroot.m,
__plt_get_axis_arg__.m, isonormals.m, isosurface.m, __fltk_file_filter__.m,
__is_function__.m, __uigetdir_fltk__.m, __uigetfile_fltk__.m,
__uiobject_split_args__.m, __uiputfile_fltk__.m, uicontextmenu.m, uiresume.m,
uiwait.m, mkpp.m, ppder.m, residue.m, addpref.m, getpref.m, ispref.m,
loadprefs.m, prefsfile.m, saveprefs.m, rmpref.m, setpref.m, fftshift.m, bicg.m,
bicgstab.m, cgs.m, gmres.m, __sprand_impl__.m, quantile.m, deblank.m,
strsplit.m, addtodate.m, bsxfun.cc, kron.cc, regexp.cc, data.cc, file-io.cc,
graphics.cc, load-save.cc, mappers.cc: Grammarcheck of documentation
before 3.6.0 release.
author | Rik <octave@nomad.inbox5.com> |
---|---|
date | Wed, 23 Nov 2011 08:38:19 -0800 |
parents | e81ddf9cacd5 |
children | 72c96de7a403 |
rev | line source |
---|---|
11523 | 1 ## Copyright (C) 2000-2011 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 -*- | |
11471
994e2a93a8e2
Use uppercase 'A' to refer to matrix inputs in m-files.
Rik <octave@nomad.inbox5.com>
parents:
11191
diff
changeset
|
20 ## @deftypefn {Function File} {} blkdiag (@var{A}, @var{B}, @var{C}, @dots{}) |
12639
4d777e05d47c
doc: Review and update documentation for "Matrix Manipulation" chapter.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
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 |
13929
9cae456085c2
Grammarcheck of documentation before 3.6.0 release.
Rik <octave@nomad.inbox5.com>
parents:
13141
diff
changeset
|
23 ## scalars. If any argument is of type sparse, the output will also be |
13118
c6601cb63e4e
Improve blkdiag for sparse matrices
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
12931
diff
changeset
|
24 ## sparse. |
c6601cb63e4e
Improve blkdiag for sparse matrices
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
12931
diff
changeset
|
25 ## @seealso{diag, horzcat, vertcat, sparse} |
5551 | 26 ## @end deftypefn |
27 | |
28 ## Author: Daniel Calvelo | |
29 ## Modified by: William Poetra Yoga Hadisoeseno | |
30 | |
31 function retval = blkdiag (varargin) | |
32 | |
33 if (nargin < 1) | |
6046 | 34 print_usage (); |
5551 | 35 endif |
36 | |
12931
cefd568ea073
Replace function handles with function names in cellfun calls for 15% speedup.
Rik <octave@nomad.inbox5.com>
parents:
12639
diff
changeset
|
37 if (! all (cellfun ("isnumeric", varargin))) |
5552 | 38 error ("blkdiag: all arguments must be numeric"); |
5551 | 39 endif |
40 | |
5552 | 41 ## Note: trailing singletons are automatically (correctly) ignored. |
5569 | 42 if (! all (cellfun ("ndims", varargin) == 2)) |
5657 | 43 error ("blkdiag: all arguments must be two-dimensional matrices"); |
5551 | 44 endif |
45 | |
5552 | 46 ## size is an option for cellfun, but it's a bit different from |
47 ## calling size directly. | |
11191
01ddaedd6ad5
Reverse changeset b1f4bdc276b6. Use all lower case for "uniformoutput" option.
Rik <octave@nomad.inbox5.com>
parents:
8920
diff
changeset
|
48 tmp = cell2mat (cellfun (@size, varargin', "uniformoutput", false)); |
5989 | 49 csz = cumsum ([0 0; tmp], 1); |
13118
c6601cb63e4e
Improve blkdiag for sparse matrices
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
12931
diff
changeset
|
50 |
c6601cb63e4e
Improve blkdiag for sparse matrices
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
12931
diff
changeset
|
51 if (any (cellfun ("issparse", varargin))) |
c6601cb63e4e
Improve blkdiag for sparse matrices
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
12931
diff
changeset
|
52 retval = sparse (csz(end,1), csz(end,2)); |
c6601cb63e4e
Improve blkdiag for sparse matrices
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
12931
diff
changeset
|
53 else |
c6601cb63e4e
Improve blkdiag for sparse matrices
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
12931
diff
changeset
|
54 retval = zeros (csz(end,:)); |
c6601cb63e4e
Improve blkdiag for sparse matrices
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
12931
diff
changeset
|
55 endif |
5592 | 56 |
57 for p = 1:nargin | |
8120
8f0150a0d19e
fix blkdiag to not rely on Matlab-incompatible behaviour
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
58 vp = varargin{p}; |
8f0150a0d19e
fix blkdiag to not rely on Matlab-incompatible behaviour
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
59 if (! isempty (vp)) |
8f0150a0d19e
fix blkdiag to not rely on Matlab-incompatible behaviour
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
60 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
|
61 endif |
5551 | 62 endfor |
63 | |
64 endfunction | |
65 | |
13118
c6601cb63e4e
Improve blkdiag for sparse matrices
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
12931
diff
changeset
|
66 ## regular tests |
5551 | 67 %!assert(blkdiag(1,ones(2),1),[1,0,0,0;0,1,1,0;0,1,1,0;0,0,0,1]) |
68 %!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]) | |
69 %!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]) | |
70 %!assert(blkdiag([1,2;3,4],[5,6,7]),[1,2,0,0,0;3,4,0,0,0;0,0,5,6,7]) | |
13118
c6601cb63e4e
Improve blkdiag for sparse matrices
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
12931
diff
changeset
|
71 ## tests involving empty matrices |
5551 | 72 %!assert(blkdiag([],[],[]),[]) |
73 %!assert(blkdiag([],[1,2;3,4],[],5,[]),[1,2,0;3,4,0;0,0,5]) | |
5601 | 74 %!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]); |
13118
c6601cb63e4e
Improve blkdiag for sparse matrices
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
12931
diff
changeset
|
75 ## tests involving sparse matrices |
c6601cb63e4e
Improve blkdiag for sparse matrices
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
12931
diff
changeset
|
76 %!assert (blkdiag (sparse([1,2;3,4]),[5,6;7,8]), sparse([1,2,0,0;3,4,0,0;0,0,5,6;0,0,7,8])) |
c6601cb63e4e
Improve blkdiag for sparse matrices
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
12931
diff
changeset
|
77 %!assert (blkdiag (sparse([1,2;3,4]),[5,6]), sparse([1,2,0,0;3,4,0,0;0,0,5,6])) |
5551 | 78 # sanity checks |
79 %!test | |
80 %! A = rand (round (rand (1, 2) * 10)); | |
81 %! assert (blkdiag (A), A); |