help-octave
[Top][All Lists]
Advanced

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

Re: num2cell


From: David Bateman
Subject: Re: num2cell
Date: Tue, 13 Sep 2005 14:34:52 +0200
User-agent: Mozilla Thunderbird 1.0.2 (Windows/20050317)

Søren Hauberg a écrit :

After John's mail I've tried num2cell.cc and it works just fine for my uses, but it has some oddities (is that a word?). I don't have access to matlab (nor do I want it) so I don't know what the "correct" behaviour is, but

num2cell("hello")

gives me a 1x5 cell of empty matrices ([]'s), which doesn't seem right. Also if num2cell is called with a struct as argument, the answer will be a cell containing an empty matrix.

I hope I'll be able to find some time this week to have a look at the code...

Thanks,
Søren

Ok, then how about the attached that deals with everything in a generic manner. The result however is that

a.b = 1; a.c = 2; num2cell(a)
error: can't perform indexing operations for struct type

which makes sense to me... I think matlab's behaviour is in error.. Another interesting odd-ball behaviour with matlab is num2cell(gf([0,1;2,3],4))
which gives 3 empty cells!!! I'd call that a bug..

D.

/*

Copyright (C) 2005 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 (num2cell, args, ,
  "-*- texinfo -*-\n\
@deftypefn {Loadable Function} address@hidden =} num2cell (@var{m})\n\
@deftypefnx {Loadable Function} address@hidden =} num2cell (@var{m}, @var{d})\n\
Convert to matrix @var{m} into a cell array. If @var{d} is defined the\n\
value @var{c} is of dimension 1 in this dimension and the elements of\n\
@var{m} are placed in slices in @var{c}.\n\
@end deftypefn\n\
@seealso{mat2cell}") 
{
  int nargin =  args.length();
  octave_value retval;

  if (nargin < 1 || nargin > 2)
    usage ("num2cell");
  else
    {
      dim_vector dv = args(0).dims ();
      Array<int> sings;

      if (nargin == 2)
        {
          ColumnVector dsings = ColumnVector (args(1).vector_value 
                                                  (false, true));
          sings.resize (dsings.length());

          if (!error_state)
            for (int i = 0; i < dsings.length(); i++)
              if (dsings(i) > dv.length() || dsings(i) < 1 ||
                  D_NINT(dsings(i)) != dsings(i))
                {
                  error ("invalid dimension specified");
                  break;
                }
              else
                sings(i) = NINT(dsings(i)) - 1;
        }

      if (! error_state)
        {
          Array<bool> idx_colon (dv.length());
          dim_vector new_dv (dv);
          octave_value_list lst (new_dv.length(), octave_value());

          for (int i = 0; i < dv.length(); i++)
            {
              idx_colon(i) = false;
              for (int j = 0; j < sings.length(); j++)
                {
                  if (sings(j) == i)
                    {
                      new_dv(i) = 1;
                      idx_colon(i) = true;
                      lst(i) = octave_value (octave_value::magic_colon_t); 
                      break;
                    }
                }
            }

          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 (int j = new_dv.length() - 1; j >= 0 ; j--)
                {
                  if (! idx_colon(j))
                    lst (j) = ii/n + 1;
                  ii = ii % n;
                  if (j != 0)
                    n /= new_dv(j-1);
                }
              ret(i) = args(0).do_index_op(lst, 0);
            }

          retval = ret;
        }
    }

  return retval;
}
          
/*
;;; Local Variables: ***
;;; mode: C++ ***
;;; End: ***
*/

reply via email to

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