C# how to convert from double[][] to double[,]

Posted on 2011-05-09
Last Modified: 2012-05-11

I was given a method that needs the input argument to be double[ , ].

Currently the data is stored a two dimensional array for another section of the application.

I haven't been able to reformat the data acording to the double [ , ]  data structure.

Where am I going wrong?


Here's my code:

                seriesArray = new double[numberOfSeries][];
                actualArray = new double[numberOfSeries][];

                for (int i = 0; i < numberOfSeries; i++)
                    seriesArray[i] = new double[list.Count];

                    actualArray[i] = new double[list.Count];

               double[,] data = new double[,] { };

                for (int i = 0; i < list.Count; i++)  
                    actualArray[i] = GetValuesByName(list[i]); 

                    double[] actualValues = actualArray[i]; 

                    for (int j = 0; j < seriesArray[i].Length; j++) 
                        seriesArray[j][i] = (double)actualValues.GetValue(j);
                        data.SetValue((double)actualSeriesValues.GetValue(j), i); // this isn't working!

Open in new window

Question by:AndyC1000

    Author Comment

    The error is "System.ArgumentException was unhandled"
      Message=Array was not a one-dimensional array.
           at System.Array.SetValue(Object value, Int32 index)
    LVL 10

    Accepted Solution

    the SetValue method needs two dimensions as the array is two-dimensional.


    data.SetValue((double)actualSeriesValues.GetValue(j), i, j);

    and also the data array will have to be initialized with a size since right now you're initializing an empty array.


    double[,] data = new double[list.Count, list.Count];
    LVL 8

    Assisted Solution

    You've only passed a single index argument, so it's expecting a single dimensional array. I think you would want to include both index values like this (untested):

    data.SetValue((double)actualSeriesValues.GetValue(j), i, j);

    Open in new window


    Featured Post

    Find Ransomware Secrets With All-Source Analysis

    Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

    Join & Write a Comment

    Bit flags and bit flag manipulation is perhaps one of the most underrated strategies in programming, likely because most programmers developing in high-level languages rely too much on the high-level features, and forget about the low-level ones. Th…
    We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now