extract a row from an NDArray

c. kingcrimson at tiscali.it
Tue May 5 00:39:10 CDT 2009


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.





More information about the Help-octave mailing list