Mercurial > hg > octave-shane
changeset 11402:a9a3ee461b83
Add second argument to Fvec
author | Judd Storrs <jstorrs@gmail.com> |
---|---|
date | Wed, 22 Dec 2010 03:43:24 -0500 |
parents | 4c335d398c8d |
children | b8b08b1ac21f |
files | src/ChangeLog src/data.cc |
diffstat | 2 files changed, 49 insertions(+), 10 deletions(-) [+] |
line wrap: on
line diff
--- a/src/ChangeLog +++ b/src/ChangeLog @@ -1,3 +1,8 @@ +2010-12-15 Judd Storrs <jstorrs@gmail.com> + + * data.cc (Fvec): New optional second argument to set dimension + of result. + 2012-12-17 Konstantinos Poulios <logari81@googlemail.com> * graphics.h.in (class axes::properties): Tag xtickmode, ytickmode
--- a/src/data.cc +++ b/src/data.cc @@ -4534,22 +4534,50 @@ DEFUN (vec, args, , "-*- texinfo -*-\n\ -@deftypefn {Built-in Function} {} vec (@var{x})\n\ +@deftypefn {Built-in Function} {} @var{v} = vec (@var{x})\n\ +@deftypefnx {Built-in Function} {} @var{v} = vec (@var{x}, @var{dim})\n\ Return the vector obtained by stacking the columns of the matrix @var{x}\n\ -one above the other. Equivalent to @code{@var{x}(:)}. Useful for functional\n\ -programming.\n\ +one above the other. Without @var{dim} this is equivalent to\n\ +@code{@var{x}(:)}. If @var{dim} is supplied, the dimensions of @var{v}\n\ +are set to @var{dim} with all elements along the last dimension.\n\ +This is equivalent to @code{shiftdim (@var{x}(:), 1-@var{dim})}.\n\ @end deftypefn") { octave_value retval; - - if (args.length () == 1) + int dim = 1; + + int nargin = args.length (); + + if (nargin < 1 || nargin > 2) + print_usage () ; + + if (! error_state && nargin == 2) + { + dim = args(1).idx_type_value (); + + if (dim < 1) + error ("vec: dim must greater than zero"); + } + + if (! error_state) { octave_value colon (octave_value::magic_colon_t); octave_value arg = args(0); retval = arg.single_subsref ("(", colon); + + + if (! error_state && dim > 1) + { + dim_vector new_dims = dim_vector::alloc (dim); + + for (int i = 0; i < dim-1; i++) + new_dims(i) = 1; + + new_dims(dim-1) = retval.numel (); + + retval = retval.reshape (new_dims); + } } - else - print_usage (); return retval; } @@ -4557,11 +4585,17 @@ /* %!assert(vec ([1, 2; 3, 4]), [1; 3; 2; 4]) -%!assert(vec ([1, 3, 2, 4]) == [1; 3; 2; 4]); +%!assert(vec ([1, 3, 2, 4]), [1; 3; 2; 4]); +%!assert(vec ([1, 2, 3, 4], 2), [1, 2, 3, 4]); +%!assert(vec ([1, 2; 3, 4]), vec ([1, 2; 3, 4], 1)); +%!assert(vec ([1, 2; 3, 4], 1), [1; 3; 2; 4]); +%!assert(vec ([1, 2; 3, 4], 2), [1, 3, 2, 4]); +%!assert(vec ([1, 3; 2, 4], 3), reshape ([1, 2, 3, 4], 1, 1, 4)); +%!assert(vec ([1, 3; 2, 4], 3), shiftdim (vec ([1, 3; 2, 4]), -2)); %!error vec (); - -%!error vec (1, 2); +%!error vec (1, 2, 3); +%!error vec ([1, 2; 3, 4], 0); */