8

I have an array of the length m*n, that stores a list of double element.

How to convert it into a matrix of m*n?

This is the method signature.

//returns a matrix of [m,n], given arr is of length m*n
static double[,] ConvertMatrix(Array arr, int m, int n)
{
}
4
  • 2
    Hhm you have to know either n or m. An array with 16 elements could be represented by 16*1, 8*2, 4*4, 2*8 and 1*16. Commented Oct 2, 2010 at 10:18
  • 1
    Hah! An array IS a matrix! Nice joke. Commented Oct 2, 2010 at 10:18
  • Yes, m and n are known. Question updated Commented Oct 2, 2010 at 10:20
  • And m and n are doubles ??? Commented Oct 2, 2010 at 10:26

3 Answers 3

14

You can use Buffer.BlockCopy to do this very efficiently:

using System;

class Test
{
    static double[,] ConvertMatrix(double[] flat, int m, int n)
    {
        if (flat.Length != m * n)
        {
            throw new ArgumentException("Invalid length");
        }
        double[,] ret = new double[m, n];
        // BlockCopy uses byte lengths: a double is 8 bytes
        Buffer.BlockCopy(flat, 0, ret, 0, flat.Length * sizeof(double));
        return ret;
    }

    static void Main()
    {
        double[] d = { 2, 5, 3, 5, 1, 6 };

        double[,] matrix = ConvertMatrix(d, 3, 2);

        for (int i = 0; i < 3; i++)
        {
            for (int j = 0; j < 2; j++)
            {
                Console.WriteLine("matrix[{0},{1}] = {2}", i, j, matrix[i, j]);
            }
        }
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Using sizeof(double) instead of plain 8 would support implementations where the double type has a different size. I don't know if such a beast exists. Yet.
@Frédéric: It's actually guaranteed to be 8 by the specification (see section 18.5.8) - but sizeof(...) would be more self-explanatory anyway.
4
private static T[,] create2DimArray<T>(T[] array, int n)
        {
            if (n <= 0)
                throw new ArgumentException("Array N dimension cannot be less or equals zero","n");
            if (array == null)
                throw new ArgumentNullException("array", "Array cannot be null");
            if (array.Length == 0)
                throw new ArgumentException("Array cannot be empty", "array");

            int m = array.Length % n == 0 ? array.Length / n : array.Length / n + 1;
            var newArr = new T[m,n];
            for (int i = 0; i < arr.Length; i++)
            {
                int k = i / n;
                int l = i % n;
                newArr[k, l] = array[i];
            }

            return newArr;
        }

Comments

3

Create the matrix and loop the items into it:

static double[,] ConvertMatrix(double[] arr, int m, int n) {
  double[,] result = new double[m, n];
  for (int i = 0; i < arr.Length; i++) {
    result[i % m, i / m] = arr[i];
  }
  return result;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.