Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Howto transpose multidimensional array in place

how to transpose a 2D matrix in place?

like image 574
Jony Avatar asked Apr 21 '10 06:04

Jony


2 Answers

Wikipedia had an article In-place matrix transposition. The article covers non-square matrices.

http://en.wikipedia.org/wiki/In-place_matrix_transposition

like image 128
dlb Avatar answered Sep 28 '22 01:09

dlb


for (int i=0; i<n; i++) {
  for (int j=0; j<i; j++) {
    temp = a[i][j];
    a[i][j] = a[j][i];
    a[j][i] = temp;
  }
}
like image 28
Simon Nickerson Avatar answered Sep 28 '22 01:09

Simon Nickerson