4

I have to use a method which accepts double[,], but I only have a double[]. How can I convert it?

Solution so far:

var array = new double[1, x.Length];
foreach (var i in Enumerable.Range(0, x.Length))
{
    array[0, i] = x;
}
Jader Dias
  • 88,211
  • 155
  • 421
  • 625

4 Answers4

9

There's no direct way. You should copy stuff into a double[,]. Assuming you want it in a single row:

double[,] arr = new double[1, original.Length];
for (int i = 0; i < original.Length; ++i) 
    arr[0, i] = original[i];
Mehrdad Afshari
  • 414,610
  • 91
  • 852
  • 789
7

If you know the width of the 2D array you can use the following to put the values in as one row after another.

    private T[,] toRectangular<T>(T[] flatArray, int width)
    {
        int height = (int)Math.Ceiling(flatArray.Length / (double)width);
        T[,] result = new T[height, width];
        int rowIndex, colIndex;

        for (int index = 0; index < flatArray.Length; index++)
        {
            rowIndex = index / width;
            colIndex = index % width;
            result[rowIndex, colIndex] = flatArray[index];
        }
        return result;
    }
ShawnFeatherly
  • 2,470
  • 27
  • 20
  • 1
    While very easy to follow, it is (relatively) computationally expensive. – cjbarth Feb 28 '13 at 22:47
  • 1
    Jon Skeet posted a performance focused method at http://stackoverflow.com/questions/5132397/fast-way-to-convert-a-two-dimensional-array-to-a-list-one-dimensional – ShawnFeatherly Apr 12 '16 at 00:29
3

I just wrote this code which I will use:

using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;

namespace MiscellaneousUtilities
{
    public static class Enumerable
    {
        public static T[,] ToRow<T>(this IEnumerable<T> target)
        {
            var array = target.ToArray();
            var output = new T[1, array.Length];
            foreach (var i in System.Linq.Enumerable.Range(0, array.Length))
            {
                output[0, i] = array[i];
            }
            return output;
        }

        public static T[,] ToColumn<T>(this IEnumerable<T> target)
        {
            var array = target.ToArray();
            var output = new T[array.Length, 1];
            foreach (var i in System.Linq.Enumerable.Range(0, array.Length))
            {
                output[i, 0] = array[i];
            }
            return output;
        }
    }
}
Jader Dias
  • 88,211
  • 155
  • 421
  • 625
1

Mehrdad assumes that the width is one since there is no real way to determine either the width or height from a one dimensional array by itself. If you have some (outside) notion of the 'width' then Mehrdad's code becomes:

// assuming you have a variable with the 'width', pulled out of a rabbit's hat
int height = original.Length / width;
double[,] arr = new double[width, height];
int x = 0;
int y = 0;
for (int i = 0; i < original.Length; ++i)
{
    arr[x, y] = original[i];
    x++;
    if (x == width)
    {
        x = 0;
        y++;
    }
}

Although, Row major is probably more common in many applications (matrices, text buffers or graphics):

// assuming you have a variable with the 'width', pulled out of a rabbit's hat
int height = original.Length / width;
double[,] arr = new double[height, width]; // note the swap
int x = 0;
int y = 0;
for (int i = 0; i < original.Length; ++i)
{
    arr[y, x] = original[i]; // note the swap
    x++;
    if (x == width)
    {
        x = 0;
        y++;
    }
}
Jared Updike
  • 7,165
  • 8
  • 46
  • 72