How to represent 2D array into 1D array ?!?


2D array

2D array

This is very interesting question “How to represents 2D array into 1D array?”. In this post I’ll show you two ways to do it:

  1. Row-Major order
  2. Column-Major order (vectorization)

Reasons to use it:

  1. When you transfer 2D array using file between applications written in different languages.
  2. When you are traversing array you will have better performance, because accessing the element from contiguous order is usually faster than accessing element from 2D array.

Examples

2d example

Now i’ll give you a short example with 2D array and how it’s represents contiguously in memory:

int[,] my2dArray = { {1,2,3}, {4,5,6} };

The result will be:

1, 2, 3

4,5,6

We have 6 numbers separated in 2 rows (3 numbers per row).

If you want to use row-major order or column-major order you must to know the exact width and height of array, because the length of array will be width * height

For example:


int[] myArray = new int[rowsNumber * columnsNumber];

Row-Major order

I use this way for representing two dimensional array. This is the formula for row-major order:

Element = currentRow * columnsNumber + currentColumn

And this is simple example:

int rowsNumber = 2, columnsNumber = 3;
 int[,] my2DArray = new int[rowsNumber, columnsNumber];
 int[] myArray = new int[rowsNumber * columnsNumber];

for (int currentRow = 0; currentRow < rowsNumber; currentRow++)
 {
 for (int currentColumn = 0; currentColumn < columnsNumber; currentColumn++)
 {
 my2DArray[currentRow , currentColumn] = int.Parse(Console.ReadLine());
 }
 }

for (int currentRow = 0; currentRow < rowsNumber; currentRow++)
 {
 for (int currentColumn = 0; currentColumn < columnsNumber; currentColumn++)
 {
 myArray[currentRow * columnsNumber + currentColumn] = my2DArray[currentRow, currentColumn];
 int value = myArray[currentRow * columnsNumber + currentColumn];
 Console.WriteLine(value);
 }
 }

Now in the memory there will be only one row with 6 elements (columns) not 2 rows with 3 elements (columns) per row.

The result is: 1, 2, 3, 4, 5, 6

*Input data: 1 2 3 4 5 6

Column-Major order (vectorization)

This type of representing is more popular as “vectorization”, because you make all columns from the array to be 1D array (vector). This is the formula for column-major order:

Element = currentColumn * rowsNumber + currentRow

int rowsNumber = 2, columnsNumber = 3;
 int[,] my2DArray = new int[rowsNumber, columnsNumber];
 int[] myArray = new int[rowsNumber * columnsNumber];

for (int currentRow = 0; currentRow < rowsNumber; currentRow++)
 {
 for (int currentColumn = 0; currentColumn < columnsNumber; currentColumn++)
 {
 my2DArray[currentRow , currentColumn] = int.Parse(Console.ReadLine());
 }
 }

for (int currentRow = 0; currentRow < rowsNumber; currentRow++)
 {
 for (int currentColumn = 0; currentColumn < columnsNumber; currentColumn++)
 {
 myArray[currentColumn * rowsNumber + currentRow] = my2DArray[currentRow, currentColumn];
 int value = myArray[currentColumn * rowsNumber + currentRow];
 Console.WriteLine(value);
 }
 }

Now in the memory there will be only one row with 6 elements (columns).

The result is: 1, 4, 2, 5, 3, 6

*Input data: 1 2 3 4 5 6

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s