convert octave Matrix object to std::vector and viceversa

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

convert octave Matrix object to std::vector and viceversa

niconeuman
Greetings,
Is there a simple and fast way of casting an octave Matrix of size (N,1) to
std::vector inside an Oct-file?
And the other way around?
Thank you very much!
Cheers,
Nicolas



--
Sent from: https://octave.1599824.n4.nabble.com/Octave-General-f1599825.html


Reply | Threaded
Open this post in threaded view
|

Re: convert octave Matrix object to std::vector and viceversa

Mike Miller-4
On Thu, Mar 12, 2020 at 16:49:49 -0500, niconeuman wrote:
> Is there a simple and fast way of casting an octave Matrix of size (N,1) to
> std::vector inside an Oct-file?

By "casting" do you mean a zero-copy way of sharing the same internal
buffer? There is no easy way to do that, and seems very unsafe.

If copying is ok, then the simplest way is

    std::copy_n (matrix.data (), matrix.numel (), vector.begin ());

> And the other way around?

    std::copy_n (vector.begin (), vector.size (), matrix.data ());

Hope that helps,

--
mike



signature.asc (849 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: convert octave Matrix object to std::vector and viceversa

niconeuman
On Fri, Mar 13, 2020 at 2:58 AM Mike Miller <[hidden email]> wrote:

>
> On Thu, Mar 12, 2020 at 16:49:49 -0500, niconeuman wrote:
> > Is there a simple and fast way of casting an octave Matrix of size (N,1) to
> > std::vector inside an Oct-file?
>
> By "casting" do you mean a zero-copy way of sharing the same internal
> buffer? There is no easy way to do that, and seems very unsafe.
>
> If copying is ok, then the simplest way is
>
>     std::copy_n (matrix.data (), matrix.numel (), vector.begin ());
>
> > And the other way around?
>
>     std::copy_n (vector.begin (), vector.size (), matrix.data ());
>
> Hope that helps,
>
> --
> mike

Thank you very much,
I will test it.

--
Dr. Nicolas Neuman

Institut für Chemie und Biochemie, Anorganische Chemie
Freie Universität Berlin,
Fabeckstraße 34–36, 14195, Berlin (Germany)

Investigador Adjunto - CIC
Instituto de Desarrollo Tecnológico para la Industria Química - INTEC
UNL-CONICET
CCT-CONICET Santa Fe
S3000ZAA Santa Fe - Santa Fe - Argentina

Jefe de Trabajos Prácticos
Departamento de Física
Fac. de Bioquímica y Ciencias Biológicas
Universidad Nacional del Litoral
Ciudad Universitaria - Paraje El Pozo
S3000ZAA Santa Fe - Santa Fe - Argentina