Unified Matrix Indexing


We all have used matrices, tables, or an array of arrays (2-dimensional array) as data structures. However, we're often faced with the very common problem of representing elements with two distinct keys while using matrices. For example, when traversing over every element of the matrix, we need to have two loops, one on rows and the other on columns. To access an element in a matrix, you need to know two keys the row index and the column index of the element. This property of matrices makes it trickier to use in certain algorithms and we end up switching the data structure. There is a way to represent the position of an element in a matrix with one value rather than using two indexes/keys.
In this article, we will unify the two keys in a matrix into a single key. The only thing is that we need to know is the number of columns in the matrix. This unified key will help us to travel from left to right, continuing for each row just like reading a book. It's a very simple technique to get a unified key. We just have to multiply the row index to the number of columns, then add it to the column index, i.e...
  1. unifiedIndex = rowIndex * numberOfColumns + columnIndex;
Now the question is: How can we get back the row and column index from this unified key? Again, it's easy. To get the row index, we just have to divide the unified key by the number of columns and the modulus of the unified key by the number of columns and this will give us the column index, i.e...
  1. rowIndex = unifiedIndex / numberOfColumns;
  2. columnIndex= unifiedIndex % numberOfColumns;
Here’s the code in C# that demonstrates the implementation and usage of a unified index:
  1. using System.Collections.Generic;  
  3. namespace UnifiedMatrixIndexing  
  4. {  
  5.     static class Program  
  6.     {  
  7.         static int GetUnifiedIndex(this int[,] matrix, int rowIndex, int columnnIndex)  
  8.         {  
  9.             return rowIndex * matrix.Length + columnnIndex;  
  10.         }  
  11.         static Dictionary<stringint> GetIndexes(this int[,] matrix, int unifiedIndex)  
  12.         {  
  13.             return new Dictionary<stringint>()  
  14.             {  
  15.                 {"ROW", unifiedIndex / matrix.Length },  
  16.                 {"COLUMN", unifiedIndex % matrix.Length }  
  17.             };  
  18.         }  
  19.         static void Main(string[] args)  
  20.         {  
  21.             int[,] matrix = new int[,] {  
  22.                 { 1, 2, 3, 4 },  
  23.                 { 5, 6, 7, 8 },  
  24.                 { 9, 10, 11, 12 }  
  25.             };  
  26.             //lets get unified index for element 10 [rowIndex: 2, columnIndex: 1]  
  27.             int unifiedIndex = matrix.GetUnifiedIndex(2, 1);  
  28.             //use unifiedIndex to traverse over every element  
  29.             Dictionary<stringint> coordinates = matrix.GetIndexes(unifiedIndex);  
  30.             //lets check whether unifiedIndex points to elment 10 or not?  
  31.             int element = matrix[coordinates["ROW"], coordinates["COLUMN"]];  
  32.             //element is 10  
  33.         }  
  34.     }  
  35. }