This project is read-only.

Problem with tranforming RDotNet.NumericMatrix to System.Double(,)

Jul 16, 2012 at 7:16 PM
Edited Jul 16, 2012 at 7:17 PM

Hey guys,

I got stuck in a problem and it would be great If someone of you could give me a hint.

 

I do some calculations with R via my .NET application and afterwards I like to transform parts of the results from the RDotNet classes to a system class. 

Transforming a NumericVector is no problem, I can do it like this:

Dim value = Engine.Evaluate("a<-c(1,2,3,4,5,6,7)").AsNumeric().ToArray

and then value is a system.double array.

 

But now I like to transform a matrix and not only a vector, so I got something like this:

Dim value = Engine.Evaluate("R code in here...").AsNumericMatrix().????

But there is no possibility to use ToArray in this case... to obtain a 2-dimensional array.

 

Any idea or workaround how I can transform the NumericMatrix to a system.double( , )???

 

Thanks a lot in advance

Dirk

 

 

Jul 16, 2012 at 11:18 PM
Edited Jul 16, 2012 at 11:19 PM

Hi Dirk,

I think this might work, but I do not have time to test.

Dim floatMatrix as RDotNet.NumericMatrix
floatMatrix = Engine.Evaluate("some R code").AsNumericMatrix()
Dim matVal(rows - 1, cols - 1) As Double
floatMatrix.CopyTo(matVal, rows, cols)

Regards,

Rob

Jul 17, 2012 at 7:40 PM

Hi Rob,

 

thanks for your support, it works :-)

 

BR

Dirk

Nov 14, 2012 at 6:10 PM

i have the same problem, and i tried the code that worked for you, but it doesn't work for me..

 

There is no "floatMatrix" that i can use?

how do i access it?

 

Nov 14, 2012 at 6:55 PM

Hi N1h1l1sT,

floatMatrix is a local variable that you must declare.

The code provided earlier was not a complete example.

So in the interest of closing this thread I will provide a complete working example (in C#).

I think the issue you are having is a less than complete understand in .NET.

Double[,] value = null;

foreach (SymbolicExpression expr1 in currentEngine.Evaluate("matrix(1:100, nrow=10)"))

{

using (RDotNet.NumericMatrix floatMatrix = expr1.AsNumericMatrix())

{

if (floatMatrix != null)

{

Int32 cols = floatMatrix.ColumnCount;

Int32 rows = floatMatrix.RowCount;

value = (Double[,])Array.CreateInstance(typeof(Double), rows, cols);

floatMatrix.CopyTo(value, rows, cols);

}

}

}

Rob

Nov 14, 2012 at 7:47 PM

it works!

 

thank you very much!! ^^