Creating and Using a 3D Matrix (Cuboid) with C#

When I write machine learning code using C#, I often have to create a 2D matrix. I have to create a 3D matrix (aka a cuboid) much less often and so I typically forget how to do so.

One rainy Sunday morning, I figured I’d refresh my memory by creating a short demo. My demo creates a 3-4-5 cuboid with default values of 0.0 and then displays the cuboid. The output is:

Begin C# cuboid (3D matrix) demo

Creating a 3-4-5 cuboid
Done

Cuboid:
  0.0  0.0  0.0  0.0  0.0
  0.0  0.0  0.0  0.0  0.0
  0.0  0.0  0.0  0.0  0.0
  0.0  0.0  0.0  0.0  0.0

  0.0  0.0  0.0  0.0  0.0
  0.0  0.0  0.0  0.0  0.0
  0.0  0.0  0.0  0.0  0.0
  0.0  0.0  0.0  0.0  0.0

  0.0  0.0  0.0  0.0  0.0
  0.0  0.0  0.0  0.0  0.0
  0.0  0.0  0.0  0.0  0.0
  0.0  0.0  0.0  0.0  0.0

End demo

The 3-4-5 cuboid is displayed as 3 blocks of 4-by-5 matrices.

No real moral to this blog post other than perhaps that writing code is a skill that must be continuously practiced. During my career, I’ve seen many examples of software engineers who were promoted to manager roles, and then, very quickly, they lost their ability to write code.



Three-dimensional arrays — cubes — often appear in mathematics and machine learning. Cubes sometimes appear in science fiction movies too.

Left: In “Kronos” (1957), aliens send a giant energy collecting cube to Earth. An attempt to stop the cube with a nuclear bomb backfires and the cube grows to an enormous size and seems unstoppable. But scientists devise a plan that forces the cube to feed on itself and Earth is saved. I give this movie a B grade.

Center: In “Star Trek: First Contact” (1996), the crew of the USS Enterprise travel back in time from the 24th century to the 21st century to stop the cybernetic Borg from conquering Earth. The Borg’s ship is a gigantic cube. The Borg are scary. I give this movie a solid B grade.

Right: In “Cube” (1997), a group of people awake to find themselves in cube-shaped rooms, with no knowledge of how they got there. Each cube-room has six doors that lead to another cube-room. Unfortunately, many of the cube-rooms contain deadly traps. Many people like this movie but I give it only a C grade.


Demo program. Replace “lt” (less than) with the Boolean operator symbol.

using System;
using System.IO;

namespace CuboidDemo
{
  internal class Program
  {
    static void Main(string[] args)
    {
      Console.WriteLine("\nBegin C# cuboid (3D matrix) demo ");

      Console.WriteLine("\nCreating a 3-4-5 cuboid ");
      double[][][] cuboid = MakeCuboid(3, 4, 5);  // all 0 values
      Console.WriteLine("Done ");

      Console.WriteLine("\nCuboid: ");
      ShowCuboid(cuboid, 1, 5);

      // double[][] matrix = MakeMatrix(3, 4);
      // ShowMatrix(matrix, 1, 5);

      Console.WriteLine("\nEnd demo ");
      Console.ReadLine();
    } // Main()

    static double[][][] MakeCuboid(int n1, int n2, int n3)
    {
      double[][][] result = new double[n1][][];
      for (int i = 0; i "lt" n1; ++i)
      {
        result[i] = new double[n2][];
        for (int j = 0; j "lt" n2; ++j)
        {
          result[i][j] = new double[n3];
        }
      }
      return result;
    }

    static void ShowCuboid(double[][][] cuboid,
      int dec, int wid)
    {
      for (int i = 0; i "lt" cuboid.Length; ++i)
      {
        for (int j = 0; j "lt" cuboid[i].Length; ++j)
        {
          for (int k = 0; k "lt" cuboid[i][j].Length; ++k)
          {
            Console.Write(cuboid[i][j][k].
              ToString("F" + dec).PadLeft(wid));
          }
          Console.WriteLine("");
        }
        Console.WriteLine("");
      }
    }

    static double[][] MakeMatrix(int nRows, int nCols)
    {
      double[][] result = new double[nRows][];
      for (int i = 0; i "lt" nRows; ++i)
        result[i] = new double[nCols];
      return result;
    }

    static void ShowMatrix(double[][] matrix,
      int dec, int wid)
    {
      for (int i = 0; i "lt" matrix.Length; ++i)
      {
        for (int j = 0; j "lt" matrix[i].Length; ++j)
        {
          Console.Write(matrix[i][j].
            ToString("F" + dec).PadLeft(wid));
        }
        Console.WriteLine("");
      }
    }

  } // class Program

} // ns
This entry was posted in Machine Learning. Bookmark the permalink.

Leave a Reply