Mercurial > hg > octave-lyh
annotate scripts/general/common_size.m @ 17535:c12c688a35ed default tip lyh
Fix warnings
author | LYH <lyh.kernel@gmail.com> |
---|---|
date | Fri, 27 Sep 2013 17:43:27 +0800 |
parents | f3d52523cde1 |
children |
rev | line source |
---|---|
14138
72c96de7a403
maint: update copyright notices for 2012
John W. Eaton <jwe@octave.org>
parents:
12931
diff
changeset
|
1 ## Copyright (C) 1995-2012 Kurt Hornik |
9477 | 2 ## Copyright (C) 2009 VZLU Prague |
10034 | 3 ## Copyright (C) 2009 Jaroslav Hajek |
3426 | 4 ## |
3922 | 5 ## This file is part of Octave. |
6 ## | |
7 ## Octave is free software; you can redistribute it and/or modify it | |
8 ## under the terms of the GNU General Public License as published by | |
7016 | 9 ## the Free Software Foundation; either version 3 of the License, or (at |
10 ## your option) any later version. | |
3426 | 11 ## |
3922 | 12 ## Octave is distributed in the hope that it will be useful, but |
2539 | 13 ## WITHOUT ANY WARRANTY; without even the implied warranty of |
14 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
3426 | 15 ## General Public License for more details. |
16 ## | |
2539 | 17 ## You should have received a copy of the GNU General Public License |
7016 | 18 ## along with Octave; see the file COPYING. If not, see |
19 ## <http://www.gnu.org/licenses/>. | |
2539 | 20 |
3369 | 21 ## -*- texinfo -*- |
6547 | 22 ## @deftypefn {Function File} {[@var{err}, @var{y1}, @dots{}] =} common_size (@var{x1}, @dots{}) |
2539 | 23 ## Determine if all input arguments are either scalar or of common |
3369 | 24 ## size. If so, @var{err} is zero, and @var{yi} is a matrix of the |
25 ## common size with all entries equal to @var{xi} if this is a scalar or | |
26 ## @var{xi} otherwise. If the inputs cannot be brought to a common size, | |
10821
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
10791
diff
changeset
|
27 ## @var{err} is 1, and @var{yi} is @var{xi}. For example: |
3426 | 28 ## |
3369 | 29 ## @example |
30 ## @group | |
31 ## [errorcode, a, b] = common_size ([1 2; 3 4], 5) | |
32 ## @result{} errorcode = 0 | |
33 ## @result{} a = [ 1, 2; 3, 4 ] | |
34 ## @result{} b = [ 5, 5; 5, 5 ] | |
35 ## @end group | |
36 ## @end example | |
3426 | 37 ## |
3369 | 38 ## @noindent |
2539 | 39 ## This is useful for implementing functions where arguments can either |
40 ## be scalars or of common size. | |
3369 | 41 ## @end deftypefn |
2539 | 42 |
5428 | 43 ## Author: KH <Kurt.Hornik@wu-wien.ac.at> |
2539 | 44 ## Created: 15 October 1994 |
45 ## Adapted-By: jwe | |
9477 | 46 ## Optimized-By: Jaroslav Hajek |
2539 | 47 |
3979 | 48 function [errorcode, varargout] = common_size (varargin) |
3426 | 49 |
2539 | 50 if (nargin < 2) |
51 error ("common_size: only makes sense if nargin >= 2"); | |
52 endif | |
53 | |
9477 | 54 ## Find scalar args. |
12931
cefd568ea073
Replace function handles with function names in cellfun calls for 15% speedup.
Rik <octave@nomad.inbox5.com>
parents:
12795
diff
changeset
|
55 nscal = cellfun ("numel", varargin) != 1; |
3426 | 56 |
9477 | 57 i = find (nscal, 1); |
58 | |
59 if (isempty (i)) | |
60 errorcode = 0; | |
3979 | 61 varargout = varargin; |
2539 | 62 else |
12931
cefd568ea073
Replace function handles with function names in cellfun calls for 15% speedup.
Rik <octave@nomad.inbox5.com>
parents:
12795
diff
changeset
|
63 match = cellfun ("size_equal", varargin, varargin(i)); |
9477 | 64 if (any (nscal &! match)) |
65 errorcode = 1; | |
66 varargout = varargin; | |
67 else | |
68 errorcode = 0; | |
69 if (nargout > 1) | |
70 scal = !nscal; | |
71 varargout = varargin; | |
10034 | 72 if (any (nscal)) |
73 dims = size (varargin{find (nscal, 1)}); | |
11191
01ddaedd6ad5
Reverse changeset b1f4bdc276b6. Use all lower case for "uniformoutput" option.
Rik <octave@nomad.inbox5.com>
parents:
11190
diff
changeset
|
74 subs = arrayfun (@ones, 1, dims, "uniformoutput", false); |
10965
28ef5a31763d
small rewrite in common_size
Jaroslav Hajek <highegg@gmail.com>
parents:
10821
diff
changeset
|
75 varargout(scal) = cellindexmat (varargin(scal), subs{:}); |
10034 | 76 endif |
2539 | 77 endif |
9477 | 78 endif |
2539 | 79 endif |
80 endfunction | |
12795
9e7ebbaf69ff
codesprint: new tests for files in scripts/general directory
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
81 |
9e7ebbaf69ff
codesprint: new tests for files in scripts/general directory
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
82 |
9e7ebbaf69ff
codesprint: new tests for files in scripts/general directory
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
83 %!test |
9e7ebbaf69ff
codesprint: new tests for files in scripts/general directory
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
84 %! m = [1,2;3,4]; |
9e7ebbaf69ff
codesprint: new tests for files in scripts/general directory
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
85 %! [err, a, b, c] = common_size (m, 3, 5); |
9e7ebbaf69ff
codesprint: new tests for files in scripts/general directory
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
86 %! assert (err, 0); |
9e7ebbaf69ff
codesprint: new tests for files in scripts/general directory
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
87 %! assert (a, m); |
9e7ebbaf69ff
codesprint: new tests for files in scripts/general directory
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
88 %! assert (b, [3,3;3,3]); |
9e7ebbaf69ff
codesprint: new tests for files in scripts/general directory
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
89 %! assert (c, [5,5;5,5]); |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
90 |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
91 %!error common_size () |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
92 |