help-octave
[Top][All Lists]
Advanced

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

Re: Toy NDArray example


From: David Bateman
Subject: Re: Toy NDArray example
Date: Wed, 18 Jan 2006 20:20:30 +0100
User-agent: Mozilla Thunderbird 0.8 (X11/20040923)

I'd think the following example is something like what you want

D.


--
David Bateman                                address@hidden
Motorola Labs - Paris +33 1 69 35 48 04 (Ph) Parc Les Algorithmes, Commune de St Aubin +33 1 69 35 77 01 (Fax) 91193 Gif-Sur-Yvette FRANCE

The information contained in this communication has been classified as: [x] General Business Information [ ] Motorola Internal Use Only [ ] Motorola Confidential Proprietary

#include <oct.h>

DEFUN_DLD(ndarraytoy, args, nargout, "y = ndarraytoy(x).")
{
  int nargin = args.length ();
  octave_value retval;

  // Important to check the number of arguments as if incorrect will seg-fault
  if (nargin != 1)
    {
      print_usage ("ndarraytoy");
      return retval;
    }

  // input matrix x is 3D. Define const so we don't make a copy later
  const NDArray x = args(0).array_value();

  if (!error_state)
    {
      // Ok it was an NDArray

      //Note if  2.1.x need  int instead of octave_idx_type
      dim_vector dv = x.dims();
      octave_idx_type len = x.length();
      
      // Define output NDArray
      NDArray y(dv);
                
      // Can only loop over one index value for NDArray. 
      for (octave_idx_type i = 0; i < len; i++) 
        y(i) = 1 / x(i);                

      retval = y;
    }
  return retval;
}




reply via email to

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