How to convert single dimensional arrays to multi dimensional ones? (C#)

20,697

Solution 1

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;
        }
    }
}

Solution 2

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];

Solution 3

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;
    }

Solution 4

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++;
    }
}
Share:
20,697
Jader Dias
Author by

Jader Dias

Perl, Javascript, C#, Go, Matlab and Python Developer

Updated on October 12, 2020

Comments

  • Jader Dias
    Jader Dias over 3 years

    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;
    }