/usr/share/octave/packages/general-1.3.4/tablify.m is in octave-general 1.3.4-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 | ## Copyright (C) 2012 Robert T. Short <octave@phaselockedsystems.com>
##
## This is free software; you can redistribute it and/or modify it under
## the terms of the GNU General Public License as published by the Free
## Software Foundation; either version 3 of the License, or (at your
## option) any later version.
##
## This software is distributed in the hope that it will be useful, but
## WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
## General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with Octave; see the file COPYING. If not, see
## <http://www.gnu.org/licenses/>.
## -*- texinfo -*-
## @deftypefn {Function File} {[@var{y1}, @dots{}] =} tablify (@var{x1}, @dots{})
##
## @noindent
## Create a table out of the input arguments, if possible. The table is
## created by extending row and column vectors to like dimensions. If
## the dimensions of input vectors are not commensurate an error will
## occur. Dimensions are commensurate if they have the same number of
## rows and columns, a single row and the same number of columns, or the
## same number of rows and a single column. In other words, vectors
## will only be extended along singleton dimensions.
##
## @noindent
## For example:
##
## @example
## @group
## [a, b] = tablify ([1 2; 3 4], 5)
## @result{} a = [ 1, 2; 3, 4 ]
## @result{} b = [ 5, 5; 5, 5 ]
## @end group
## @end example
## @example
## @group
## [a, b, c] = tablify (1, [1 2 3 4], [5;6;7])
## @result{}
## b = [ 1 1 1 1; 1 1 1 1; 1 1 1 1]
## @result{} b = [ 1 2 3 4; 1 2 3 4; 1 2 3 4]
## @result{} c = [ 5 5 5 5; 6 6 6 6; 7 7 7 7 ]
## @end group
## @end example
##
## @noindent
## The following example attempts to expand vectors that do not have
## commensurate dimensions and will produce an error.
##
## @example
## @group
## tablify([1 2],[3 4 5])
## @end group
## @end example
##
## @noindent
## Note that use of array operations and broadcasting is more efficient
## for many situations.
##
## @seealso {common_size}
##
## @end deftypefn
## Author: Robert T. Short
## Created: 3/6/2012
function [varargout] = tablify (varargin)
if (nargin < 2)
varargout = varargin;
return;
endif
empty = cellfun (@isempty, varargin);
nrows = cellfun (@rows, varargin(~empty));
ridx = nrows>1;
if (any(ridx))
rdim = nrows(ridx)(1);
else
rdim = 1;
endif
ncols = cellfun (@columns, varargin(~empty));
cidx = ncols>1;
if (any(cidx))
cdim = ncols(cidx)(1);
else
cdim = 1;
endif
if ( any(nrows(ridx) != rdim ) || any(ncols(cidx) != cdim ))
error('tablify: incommensurate sizes');
endif
varargout = varargin;
varargout(~ridx) = cellindexmat(varargout(~ridx),ones(rdim,1),':');
varargout(~cidx) = cellindexmat(varargout(~cidx),':',ones(1,cdim));
endfunction
%!error tablify([1,2],[3,4,5])
%!error tablify([1;2],[3;4;5])
%!test
%! a = 1; b = 1; c = 1;
%! assert(tablify(a),a);
%! [x,y,z]=tablify(a,b,c);
%! assert(x,a);
%! assert(y,b);
%! assert(z,c);
%!test
%! a = 1; b = [1 2 3];
%! [x,y]=tablify(a,b);
%! assert(x,[1 1 1]);
%! assert(y,[1 2 3]);
%!test
%! a = 1; b = [1;2;3];
%! [x,y]=tablify(a,b);
%! assert(x,[1;1;1]);
%! assert(y,[1;2;3]);
%!test
%! a = [1 2]; b = [1;2;3]; c=1;
%! [x,y,z]=tablify(a,b,c);
%! assert(x,[1 2; 1 2; 1 2]);
%! assert(y,[1 1; 2 2; 3 3]);
%! assert(z,[1 1; 1 1; 1 1]);
%!test
%! a = [1 2]; b = [1;2;3]; c=[2 3];
%! [x,y,z]=tablify(a,b,c);
%! assert(x,[1 2; 1 2; 1 2]);
%! assert(y,[1 1; 2 2; 3 3]);
%! assert(z,[2 3; 2 3; 2 3]);
%!test
%! a = [1 2]; b = [1;2;3]; c=[];
%! [x,y,z]=tablify(a,b,c);
%! assert(x,[1 2; 1 2; 1 2]);
%! assert(y,[1 1; 2 2; 3 3]);
%! assert(z,[]);
|