octave-maintainers
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: C++ version of regexprep.cc


From: Tom Holroyd (NIH/NIMH) [E]
Subject: Re: C++ version of regexprep.cc
Date: Tue, 02 May 2006 09:48:22 -0400
User-agent: Mozilla Thunderbird 1.0.7-1.1.fc3 (X11/20050929)



tag = cell(number_of_tags,4);
tag{:,1} = mat2cell (xml, 1, tag_end - tag_start);

I get inconsistent dimensions for that; mat2cell must return the entire matrix, 
it can't do slices.  But you can get groups of adjacent columns or rows easily.

I fixed up the help a little in this version, but it's still somewhat 
confusing.  How about adding an example, like:

octave:1> x = rand(3)
x =

 0.088421  0.036614  0.176033
 0.771654  0.263224  0.700347
 0.582790  0.709729  0.513203

octave:2> mat2cell (x, [2,1])
ans =

{
 [1,1] =

   0.088421  0.036614  0.176033
   0.771654  0.263224  0.700347

 [2,1] =

   0.58279  0.70973  0.51320

}

--
Tom Holroyd, Ph.D.
I would dance and be merry,
Life would be a ding-a-derry,
If I only had a brain.
       -- The Scarecrow
/*

Copyright (C) 2006 David Bateman

This program 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 2, or (at your option) any
later version.

This program 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 this program; see the file COPYING.  If not, write to the Free
Software Foundation, 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

In addition to the terms of the GPL, you are permitted to link
this program with any Open Source program, as defined by the
Open Source Initiative (www.opensource.org)

*/

#include <octave/config.h>
#include <octave/oct.h>
#include <octave/Cell.h>
#include <octave/ov-str-mat.h>
#include <octave/ov-colon.h>

DEFUN_DLD (mat2cell, args, ,
  "-*- texinfo -*-\n\
@deftypefn {Loadable Function} address@hidden =} mat2cell (@var{a}, @var{m}, 
@var{n})\n\
@deftypefnx {Loadable Function} address@hidden =} mat2cell (@var{a}, @var{d1}, 
@var{d2}, @dots{})\n\
@deftypefnx {Loadable Function} address@hidden =} mat2cell (@var{a}, @var{r})\n\
Converts the matrix @var{a} to a cell array @var{b}. Input requirements: if 
@var{a} is 2-D, then\n\
@code{sum (@var{m}) == size (@var{a}, 1)} and  @code{sum (@var{n}) ==\n\
size (@var{a}, 2)}. Similarly if @var{a} is multi-dimensional and the\n\
number of dimensional arguments is equal to the dimensions of @var{a}.\n\
\n\
Given a single dimensional argument @var{r}, the other dimensional\n\
arguments are assumed to equal @code{size (@var{a},@var{i})}.\n\
@end deftypefn\n\
@seealso{num2cell,cell2mat}")
{
  int nargin = args.length();
  octave_value retval;

  if (nargin < 2)
    usage ("mat2cell");
  else
    {
      dim_vector dv = args(0).dims();
      dim_vector new_dv;
      new_dv.resize(dv.length());

      if (nargin > 2)
        {
          if (nargin - 1 != dv.length())
            error ("mat2cell: Incorrect number of dimensions");
          else
            {
              for (octave_idx_type j = 0; j < dv.length(); j++)
                {
                  ColumnVector darg = ColumnVector (args(j+1).vector_value
                                                    (false, true));

                  double sumdarg = 0.;
                  for (octave_idx_type i = 0; i < darg.length(); i++)
                    {
                      if (darg(i) >= 0)
                        sumdarg += darg(i);
                      else
                        {
                          error ("mat2cell: invalid dimensional argument");
                          break;
                        }
                    }

                  if (sumdarg != dv(j))
                    error ("mat2cell: inconsistent dimensions");

                  if (error_state)
                    break;

                  new_dv(j) = darg.length();
                }
            }

          if (! error_state)
            {
              octave_value_list lst (new_dv.length(), octave_value());
              Cell ret (new_dv);
              octave_idx_type nel = new_dv.numel();
              octave_idx_type ntot = 1;

              for (int j = 0; j < new_dv.length()-1; j++)
                ntot *= new_dv(j);

              for (octave_idx_type i = 0; i <  nel; i++)
                {
                  octave_idx_type n = ntot;
                  octave_idx_type ii = i;
                  for (octave_idx_type j =  new_dv.length() - 1;  j >= 0; j--)
                    {
                      ColumnVector d = ColumnVector (args(j+1).vector_value
                                                     (false, true));

                      double idx = 0.;
                      for (octave_idx_type k = 0; k < ii/n; k++)
                        idx += d(k);
                      lst (j) = Range(idx + 1, idx + d(ii/n));
                      ii = ii % n;
                      if (j != 0)
                        n /= new_dv(j-1);
                    }
                  ret(i) = args(0).do_index_op(lst, 0);
                  if (error_state)
                    break;
                }

              if (!error_state)
                retval = ret;
            }
        }
      else
        {
          ColumnVector d = ColumnVector (args(1).vector_value
                                         (false, true));

          double sumd = 0.;
          for (octave_idx_type i = 0; i < d.length(); i++)
            {
              if (d(i) >= 0)
                sumd += d(i);
              else
                {
                  error ("mat2cell: invalid dimensional argument");
                  break;
                }
            }

          if (sumd != dv(0))
            error ("mat2cell: inconsistent dimensions");

          new_dv(0) = d.length();
          for (octave_idx_type i = 1; i < dv.length(); i++)
            new_dv(i) = 1;

          if (! error_state)
            {
              octave_value_list lst (new_dv.length(), octave_value());
              Cell ret (new_dv);

              for (octave_idx_type i = 1; i < new_dv.length(); i++)
                lst (i) = Range (1., static_cast<double>(dv(i)));

              double idx = 0.;
              for (octave_idx_type i = 0; i <  new_dv(0); i++)
                {
                  lst(0) = Range(idx + 1., idx + d(i));
                  ret(i) = args(0).do_index_op(lst, 0);
                  idx += d(i);
                  if (error_state)
                    break;
                }

              if (!error_state)
                retval = ret;
            }
        }
    }

  return retval;
}

/*
;;; Local Variables: ***
;;; mode: C++ ***
;;; End: ***
*/

reply via email to

[Prev in Thread] Current Thread [Next in Thread]