I have a vector of size n
; n
is power of 2. I need to treat this vector as a matrix n
= R
*C
. Then I need to transpose the matrix.
For example, I have vector: [1,2,3,4,5,6,7,8]
I need to find R and C. In this case it would be: 4,2. And treat vector as matrix:
[1,2]
[3,4]
[5,6]
[7,8]
Transpose it to:
[1, 3, 5, 7]
[2, 4, 6, 8]
After transposition vector should be: [1, 3, 5, 7, 2, 4, 6, 8]
Is there existing algorithms to perform in-place non-square matrix transposition? I don't want to reinvent a wheel.
My vector is very big so I don't want to create intermediate matrix. I need an in-place algorithm. Performance is very important.
- All modofications should be done in oroginal vector. Ideally algorithm should work with chunks that will fit in CPU cache.
- I can't use iterator because of memory locality. So I need real transposition.
- It does not matter if matrix would be 2x4 or 4x2