extract a row from an NDArray

Carlo de Falco carlo.defalco at gmail.com
Tue May 5 03:03:15 CDT 2009


On 5 May 2009, at 07:39, c. wrote:

>
> On 5 May 2009, at 07:27, Jaroslav Hajek wrote:
>
>> On Tue, May 5, 2009 at 7:03 AM, c. <kingcrimson at tiscali.it> wrote:
>>> Hi,
>>> What is the best way to extract a row from an NDArray in an .oct  
>>> file?
>>> I am looking for a way to translate an m-file containing
>>>
>>> w    = squeeze(coefs(4,:,:))
>>>
>>> into C++.
>>> Thanks,
>>> Carlo
>>>
>>
>> Indexing by more than two indices requires building an index array
>> (C++ offers no good way to create methods with arbitrary number of
>> arguments).
>>
>> Array<idx_vector> idx(3, idx_vector::colon); idx(0) = 4;
>> NDArray w = coefs.index (idx).squeeze ();
>>
>> cheers
>>
>
> Jaroslav, thanks!
> This seems to work only with 3.1 though:
>
> -----------------------
> #include <iostream>
> #include <octave/oct.h>
> DEFUN_DLD(provacc, args, nargout,"\
> provacc:  Undocumented private function\
> ")
> {
>
>  octave_value_list retval;
>  const NDArray coefs = args(0).array_value ();
>  if (!error_state)
>    {
>      Array<idx_vector> idx(3, idx_vector::colon); idx(0) = 1;
>      NDArray w = coefs.index (idx).squeeze ();
>      retval(0) = octave_value(w);
>    }
>  return retval;
> }
> -----------------------
>
> $ mkoctfile provacc.cc
> provacc.cc: In function ‘octave_value_list Fprovacc(const  
> octave_value_list&, int)’:
> provacc.cc:16: error: ‘colon’ is not a member of ‘idx_vector’
> provacc.cc:17: error: conversion from ‘ArrayN<double>’ to non-scalar  
> type ‘NDArray’ requested
>
> $ /opt/octave/3.1/bin/mkoctfile provacc.cc
> $
> -----------------------
>
> is there a way to do this that is compatible with the 3.0 release as  
> well?
> c.

To answer my own question, the following seems to work with 3.0.5:

----
#include <octave/oct.h>

DEFUN_DLD(provacc, args, nargout,"\
  provacc:  Undocumented private function\
")
{

   octave_value_list retval;
   const NDArray coefs = args(0).array_value();
   if (!error_state)
     {
       Array<idx_vector> idx(3, idx_vector(':')); idx(0) = 1;
       NDArray w (coefs.index (idx).squeeze ());
       retval(0) = octave_value(w);
     }
   return retval;
}
----

is this correct? Will it also work in the 3.2, I'd really like to keep  
compatibility with both 3.1 and 3.0...
c.












More information about the Help-octave mailing list