## Create a matrix directly from an array of doubles?

hughperkins
Registered Member
Posts
6
Karma
0

### Create a matrix directly from an array of doubles?

Mon Oct 08, 2012 12:48 am
Hi,

I have an array of doubles, and I want to create a MatrixXd from this. Is there any way of doing this without having to loop over the array and assign each value to the matrix?

How does eigen store the matrix internally? A single array of doubles, or a 2d array?
Dee33
Registered Member
Posts
54
Karma
0
OS

### Re: Create a matrix directly from an array of doubles?

Mon Oct 08, 2012 7:03 am
A matrix is stored internally as single array, one column after another (for Column-major)
http://eigen.tuxfamily.org/dox-devel/QuickRefPage.html
hughperkins
Registered Member
Posts
6
Karma
0

### Re: Create a matrix directly from an array of doubles?

Mon Oct 08, 2012 9:32 am
Ok, that gets me a ColXpr object? Then, that has a public pointer to the underlying array, so I can memcpy onto that?

Can I get a pointer to the very first element of the matrix, for example by doing &mat(0,0) and memcpy the entire array, all columns in one go, onto that?

Is there going to be any speed advantage of doing this, or is using a column-major loop to copy the elements one by one from the array via the mat(r,c) operator going to be about the same speed anyway?
manuels
Registered Member
Posts
47
Karma
0

### Re: Create a matrix directly from an array of doubles?

Mon Oct 08, 2012 9:36 am
have a look at "Mapping external arrays" on http://eigen.tuxfamily.org/dox-devel/QuickRefPage.html
hughperkins
Registered Member
Posts
6
Karma
0

### Re: Create a matrix directly from an array of doubles?

Mon Oct 08, 2012 12:30 pm
Super! That was what I was looking for.

Jeigen wrapping overhead for 1000x1000 matrices is now down from 5.4% to 4.1%. https://github.com/hughperkins/jeigen/b ... jeigen.cpp

Anything similar for sparse matrices?
Dee33
Registered Member
Posts
54
Karma
0
OS

### Re: Create a matrix directly from an array of doubles?

Tue Oct 09, 2012 6:58 am
For sparse matrices, you have the class MappedSparseMatrix http://eigen.tuxfamily.org/dox-devel/cl ... atrix.html
Note that the Map classes use the same memory as the arrays you supply. So you should not free those arrays before you finish with the Map classes.

## Who is online

Registered users: 16777216, alake, Baidu [Spider], Bing [Bot], Exabot [Bot], garthecho, Google [Bot], GordieGii, Hans, koriun, Majestic-12 [Bot], mmistretta, nezumi, onesandzeros, Yahoo [Bot]