If I'm given the matrix:
A B C
D E F
G H I
and it is stored in a vector by rows:
[A, B, C, D, E, F, G, H, I]
How can I use the above vector with a for loop to store the transposed version in another vector.
[A, D, G, B, E, H, C, F, I]
If I'm given the matrix:
A B C
D E F
G H I
and it is stored in a vector by rows:
[A, B, C, D, E, F, G, H, I]
How can I use the above vector with a for loop to store the transposed version in another vector.
[A, D, G, B, E, H, C, F, I]
You have 3 rows with 3 elements (because number of columns is 3) in the old vector and you will have 3 columns with 3 elements (because number of rows is 3) in the new vector.
Therefore, it can be realized like this:
int nRows = 3, nCols = 3;
for (int i = 0; i < nRows * nCols; i++) {
int r = i / nCols, c = i % nCols;
new[c * nRows + r] = old[r * nCols + c];
}