help-octave
[Top][All Lists]
Advanced

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

Re: Cast Matrix to C++ array


From: Andy Buckle
Subject: Re: Cast Matrix to C++ array
Date: Wed, 5 Jan 2011 09:37:21 +0000

On Wed, Jan 5, 2011 at 9:07 AM, Surfing <address@hidden> wrote:
> Thanks....btw that method returns a mono-dimensional array. Isn't there a
> way to access it as a matrix? (so with mat[row][column])
>
> Thanks
>
> Il 05/01/2011 09:19, Andy Buckle ha scritto:
>>
>> On Wed, Jan 5, 2011 at 7:26 AM, Surfing<address@hidden>  wrote:
>>>
>>> Hi all,
>>> is there a way to do a "fast" cast from a Matrix octave type to an C++
>>> bi-dimensional double array?
>>> Or the only way is doing it element by element?
>>
>> "
>> — Method: T* fortran_vec (void)
>>
>>     This method returns a pointer to the underlying data of the matrix
>> or a array so that it can be manipulated directly, either within
>> Octave or by an external library.
>> "
>>
>>
>> http://www.gnu.org/software/octave/doc/interpreter/Matrices-and-Arrays-in-Oct_002dFiles.html#Matrices-and-Arrays-in-Oct_002dFiles
>>

(Please reply-to-all, and post new stuff after the copy of the previous email)

I think Octave stores stuff internally as 1D. I think this sort of
thing is your best option

#include "octave/oct.h"

DEFUN_DLD (array_dim, args, nargout,"") {
  octave_value_list retval;

  const int rows=3, cols=4;

  dim_vector dv(rows,cols);
  uint32NDArray arr(dv);
  uint32_t * pix_p=(uint32_t *)arr.fortran_vec();

  for (uint32_t i=0; i<rows*cols ;i++) {
    pix_p[i] = i;
  }

  printf("%i\n",pix_p[1+rows*2]); // (2,3)

  return octave_value(arr);
}

-- 
/* andy buckle */


reply via email to

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