0

I have the following matrix:

const double values[3][41] = { { -116.99, -116.99, -116.99, -116.99, -116.99,
        -116.99, -116.99, -116.99, -116.99, -116.99, -116.99, -116.99, -116.99,
        -116.99, -116.99, -116.99, -116.99, -116.99, -116.99, -72.385, -60.657,
        -72.385, -116.99, -116.99, -116.99, -116.99, -116.99, -116.99, -116.99,
        -116.99, -116.99, -116.99, -116.99, -116.99, -116.99, -116.99, -116.99,
        -116.99, -116.99, -116.99, -116.99 }, { -116.99, -116.99, -116.99,
        -116.99, -116.99, -116.99, -116.99, -116.99, -116.99, -116.99, -116.99,
        -116.99, -116.99, -116.99, -116.99, -116.99, -116.99, -97.597, -81.568,
        -74.745, -66.323, -74.745, -81.568, -97.597, -116.99, -116.99, -116.99,
        -116.99, -116.99, -116.99, -116.99, -116.99, -116.99, -116.99, -116.99,
        -116.99, -116.99, -116.99, -116.99, -116.99, -116.99 },
        { -116.99, -116.99, -116.99, -116.99, -116.99, -116.99, -116.99,
                -116.99, -116.99, -116.99, -116.99, -116.99, -116.99, -116.99,
                -116.99, -107.26, -97.836, -89.451, -83.642, -78.459, -71.99,
                -78.459, -83.642, -89.451, -97.836, -107.26, -116.99, -116.99,
                -116.99, -116.99, -116.99, -116.99, -116.99, -116.99, -116.99,
                -116.99, -116.99, -116.99, -116.99, -116.99, -116.99 }}

Is there a quick way to transform it to values[3][41] while preserving correctness of the values

These are values coming from a device which has written the output as [y][x] instead of [x][y]. I though of writing a wrapped function for invoking values from the matrix but maybe there is a way to invert the axes.

Striezel
  • 3,693
  • 7
  • 23
  • 37
Kristof Pal
  • 966
  • 4
  • 12
  • 28
  • There is no such way. Your approach is correct one (apart from 'physically' transform a matrix) – Serge Sep 03 '16 at 19:01
  • Perhaps see http://stackoverflow.com/questions/16737298/what-is-the-fastest-way-to-transpose-a-matrix-in-c but a wrapper function sounds good as you won't need to move any data around. – robor Sep 03 '16 at 19:02
  • You should show us how the values are written from the device into the actual array. – maja Sep 03 '16 at 19:30

1 Answers1

0

You just have to loop over it:

const double values[3][41] = {...};
double newValues[41][3];

for(int i=0; i<3; ++i) {
    for(int j=0; j<41; ++i) {
        newValues[j][i] = values[i][j];
    }
}

However, this requires twice as much memory space. It would also be possible to reorder it in-memory.

maja
  • 17,250
  • 17
  • 82
  • 125