# Creating a matrix with LibraryLink

**URL:** <https://community.wolfram.com/t/creating-a-matrix-with-librarylink/1345>\
**Category:** Ideas\
**Tags:** external-interfaces\
**Created:** [January 23, 2014, 6:39pm UTC](https://community.wolfram.com/t/creating-a-matrix-with-librarylink/1345 "2014-01-23T18:39:39Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![arnoudb](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/arnoudb/32/41177_2.png) [@arnoudb](https://community.wolfram.com/u/arnoudb)\
**Post date:** [January 23, 2014, 6:39pm UTC](https://community.wolfram.com/t/creating-a-matrix-with-librarylink/1345/1 "2014-01-23T18:39:39Z")

</div>

In a [previous post](https://wolfram.discourse.group/t/1342/1) I explained how to work with passing in a list of data and calculating its mean value.  
In this post we are going to pass in two arguments, a row and column value, and return a matrix with that  
specified dimension.

Here is the code (CreateMatrix.c):

```auto
#include "WolframLibrary.h"

DLLEXPORT int CreateMatrix(WolframLibraryData libData, mint Argc, MArgument *Args, MArgument Res) {

        mint row,col,rows,cols,count;

        MTensor out;
        mint out_type = MType_Integer;
        mint out_rank = 2 ;
        mint out_dims[2];
        mint* out_data;
        int err;

        rows = MArgument_getInteger(Args[0]);
        cols = MArgument_getInteger(Args[1]);
        out_dims[0] = rows;
        out_dims[1] = cols;

        err = libData->MTensor_new( out_type, out_rank, out_dims, &out );
        out_data = libData->MTensor_getIntegerData(out);

        count=1;
        for(row=0; row<rows; row++) {
                for(col=0; col<cols; col++) {
                        out_data[col+cols*row] = count++;
                }
        }

        MArgument_setMTensor(Res,out);
        return LIBRARY_NO_ERROR;
}

```

The key new functionality in this code is the creation of an MTensor using libData-\>MTensor\_new and filling the  
rows and columns of that tensor with (increasing) integer values, using ‘count’.

To create a new tensor you need to specify the type (MType\_Integer), the rank (2 for a matrix) and the dimensions (rows and cols in this case).

To fill the matrix you need to get the (uninitialized) integer data out of the tensor, using libData-\>MTensor\_getIntgerData. Then you can fill the data with a nested for loop over the rows and columns.

The Wolfram Language build and function loading code will now need to specify that we are going to pass in two arguments, a rows  
and a columns argument and that we are going to return a 2 dimensional integer tensor (CreateMatrix.m):

```auto
Needs["CCompilerDriver`"];
lib = CreateLibrary[{"CreateMatrix.c"},"CreateMatrix"];
CreateMatrix = LibraryFunctionLoad[lib,"CreateMatrix",{Integer,Integer},{Integer,2}];

```

Now we can try running this code in the Wolfram Language:

```auto
Mathematica 10.0 for Microsoft Windows (64-bit)
Copyright 1988-2013 Wolfram Research, Inc.

In[1]:= Get["CreateMatrix.m"]

In[2]:= CreateMatrix[3,4] // Grid

Out[2]= 1 2 3 4

        5 6 7 8

        9 10 11 12

In[3]:= CreateMatrix[3,12] // Grid

Out[3]= 1 2 3 4 5 6 7 8 9 10 11 12

        13 14 15 16 17 18 19 20 21 22 23 24

        25 26 27 28 29 30 31 32 33 34 35 36

```

In the next post we will take a look at how to [work with complex numbers](http://community.wolfram.com/groups/-/m/t/189709) .

---

<div class="post-metadata">

**Author:** ![saur12ab](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/saur12ab/32/15904_2.png) [@saur12ab](https://community.wolfram.com/u/saur12ab)\
**Post date:** [October 17, 2016, 3:14am UTC](https://community.wolfram.com/t/creating-a-matrix-with-librarylink/1345/2 "2016-10-17T03:14:13Z")

</div>

Dear Arnoud,

How to adapt this code to work for C++ too?

* * *

Just found out!

Replace:

```
DLLEXPORT int CreateMatrix(WolframLibraryData libData, mint Argc, MArgument *Args, MArgument Res)

```

with

```
extern "C" __declspec(dllexport) 
int CreateMatrix(WolframLibraryData libData, mint Argc, MArgument *Args, MArgument Res)

```

---

<div class="post-metadata">

**Author:** ![saur12ab](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/saur12ab/32/15904_2.png) [@saur12ab](https://community.wolfram.com/u/saur12ab)\
**Post date:** [October 17, 2016, 10:30am UTC](https://community.wolfram.com/t/creating-a-matrix-with-librarylink/1345/3 "2016-10-17T10:30:09Z")

</div>

Seemingly an easy task: How to adapt the code above to produce a matrix of real numbers?

Here are the changes I have done so far:

```
#include "WolframLibrary.h"

extern "C" __declspec(dllexport) 
int CreateMatrix(WolframLibraryData libData, mint Argc, MArgument *Args, MArgument Res) {
     
        mint rows,cols;
 
        MTensor out;
        mint out_type = MType_Real;
        mint out_rank = 2 ;
        mint out_dims[2];
        mreal* out_data;
        int err;

        rows = MArgument_getInteger(Args[0]);
        cols = MArgument_getInteger(Args[1]);
        out_dims[0] = rows;
        out_dims[1] = cols;

        err = libData->MTensor_new( out_type, out_rank, out_dims, &out );
        out_data = libData->MTensor_getRealData(out);

        for(int row=0; row<rows; row++) {
                for(int col=0; col<cols; col++) {
                        out_data[col+cols*row] = 1.1;
                }
        }

        MArgument_setMTensor(Res,out);
        return LIBRARY_NO_ERROR;
}

```

* * *

Missed completely that there are other specification functions. here was my problem:

```
CreateMatrix = LibraryFunctionLoad[lib, "CreateMatrix", {Integer, Integer}, {Real, 2}];

```

---

<div class="post-metadata">

**Author:** ![jostadler](https://avatars.discourse-cdn.com/v4/letter/j/b487fb/32.png) [@jostadler](https://community.wolfram.com/u/jostadler)\
**Post date:** [March 10, 2019, 5:35pm UTC](https://community.wolfram.com/t/creating-a-matrix-with-librarylink/1345/4 "2019-03-10T17:35:33Z")

</div>

You also can use:

```
EXTERN_C DLLEXPORT int CreateMatrix(WolframLibraryData libData, mint Argc, MArgument *Args, MArgument Res)

```
