creating an matrix from C++ array

John Swensen jpswensen at comcast.net
Sat Apr 4 08:39:19 CDT 2009


On Apr 4, 2009, at 3:23 AM, Jaroslav Hajek wrote:

> On Sat, Apr 4, 2009 at 8:31 AM, Martijn  
> <martijn.brouwer at inter.nl.net> wrote:
>> Hi,
>> I would like to create an octave matrix (of uint16) from an C++  
>> array,
>> but there is apparently no constructor that accepts this:
>>
>>        unsigned a[11];
>>        for(int i=0; i<11; i++)
>>        {       a[i]=i;
>>        }
>>        uint16NDArray A(a,11);
>>
>> Do I have to loop over all elements or is there a quicker way?
>> I want to used such construction to read a lot of uint16 data from a
>> file into an C++ array of unsigned shorts. Then I would like to  
>> create
>> an octave uint16NDArray from the array.
>>
>
> In short, no, there's no such constructor. If you want to avoid
> copying the elements, use uint16NDArray to manage your data from the
> very start.
>
> regards
>
> -- 
> RNDr. Jaroslav Hajek
> computing expert & GNU Octave developer
> Aeronautical Research and Test Institute (VZLU)
> Prague, Czech Republic
> url: www.highegg.matfyz.cz

I while ago, I wanted to do the same thing.  I found something that  
might not be quite as clean, but was definitely as fast as one would  
expect.
Matrix m1(settings.width, settings.height);
uint8NDArray m = octave_value(m1).uint8_array_value();
octave_uint8* tmp = m.fortran_vec();
memcpy( tmp, capturebuffer, settings.height*settings.width );
return octave_value(m.transpose());
Note that my data was row major indexing and fotran_vec is column  
major indexing (I think).  That is the reason I made my Matrix with  
the width and height swapped and then returned the transpose.  I don't  
have the code I am talking about in front of me, just this snippet  
from an old email.

John Swensen


More information about the Help-octave mailing list