Search code examples
c++matrixindices

Custom indexes with C++ matrix


I would like to treat C++ bi-dimensional arrays (matrices) in C++ as I can do with R data-frames. What I mean by that is to be able to specify indexes values for the matrices.

For example the natural C++ integer matrix is like this:

  0 1 2 3 4 ...
0 1 0 1 0 .
1 3 . . .
2 8 . .
3 . .
4 .
.
.
.

I would like to specify indexes in matrix, so they would be like this, for example:

  5 7 8 13 24 ...
0 1 0 1 0 .
1 3 . . .
2 8 . .
6 . .
8 .
.
.
.

Any advise will be much appreciated.


Solution

  • If you want to switch columns, rows of matrices, you can use some indirection:

     indexTable[0][0] = 0; // map row index 0 to 0
     indexTable[1][0] = 5; // map column index 0 to 5
    

    and use it like this:

     value = matrix[indexTable[0][RowIndex]][indexTable[1][ColumnIndex];
    

    or you can write a class to handle this indirection for you.