shraga
shraga

Reputation: 59

how to convert object array to double array in c#

Hello recently i am trying get the result using matlab function and matlab function can return double array like image but and then I got that double by object class in C# but I could not convert to double that object class some one could help me

I have solved this problem

        MLApp.MLApp matlab = new MLApp.MLApp();
        matlab.Execute(@"Path");
        object result = null;
        matlab.Feval("RemoveShadow", 1, out result, 12, 13);
        var res = (result as object[]).Select(x => (double[,])x).ToArray();
        object im = res.GetValue(0);
        double[,] d = (double[,])im;

I have solved this problem

Upvotes: 5

Views: 6692

Answers (3)

Farhad Jabiyev
Farhad Jabiyev

Reputation: 26635

You can try this to convert object[] to double[]:

var res = (result as object[]).Select(x => (double)x).ToArray();

Upvotes: 2

Tazeem Ansari
Tazeem Ansari

Reputation: 1

Correct way to handle this is

var res = (result as object[]).Select(x => Convert.ToDouble(x)).ToArray();

Upvotes: -1

Pranay Rana
Pranay Rana

Reputation: 176906

if all are double in object array than, alternative to above answer

double[] resultArray = Array.ConvertAll<object, double>
                                  (inputArray, x => (double)x);

Upvotes: 6

Related Questions