Take the 2-minute tour ×

So I have a ONE dimensional array with N values, where N is a perfect square. I visualize this one dimensional array as a two dimensional array (although it is not). For example, an array with values int Array = { 0,1,2,3,4,5,6,7,8 }

That is

int *Array = new int [9];                                                                                                                                                                                                    
         Array[i] = i; // For example

This is printed as

0 1 2
3 4 5 
6 7 8

So, I want to interchange the position in the one dimensional array such that I get the transpose of it,...

For example...

0 3 6
1 4 7
2 5 8

This is basically the same one dimensional array , but the values are swapped such that the array is now int Array = {0,3,6,1,4,7,2,5,8}

If I were to scale it to an array of dimension 1024*1024, how will the logic be ?

share|improve this question

6 Answers 6

up vote 18 down vote accepted

With n = sqrt(N), you could just try something simple like:

        std::swap(Array[n*i + j], Array[n*j + i]);
share|improve this answer
Haha. That was a super fast answer ;) –  Legolas Oct 2 '11 at 21:43

The transpose operation performs swap(v[y][x],v[x][y]) for the upper or lower triangle excluding the diagonal of the matrix, (let's say upper).

In a C one-dimensional vector vc, v[y][x] corresponds to vc[y*n+x]. So you want to do vc[y*n+x] = vc[x*n+y]

The elements you want to swap are the ones for which x > y.

You end up doing:

    for(int x = y+1; x < n; ++x)
        swap(vc[x*n + y], vc[y*n + x]);

You could have figured this yourself...

share|improve this answer
True. Thanks for quick answer ! ;) –  Legolas Oct 2 '11 at 21:44
#include <iostream>
#include <cmath>

using namespace std;

int xyToIndex(const int x, const int y, const int size){
    return x + y * size;

int main(){
    int a[] = { 0,1,2,3,4,5,6,7,8 };

    const int size = sqrt(sizeof(a)/sizeof(int));

    //print only
            cout << a[xyToIndex(x,y,size)] << " ";;
        cout << endl;
    //make array
    int b[size*size];
    int index = 0;
             b[index++] = a[xyToIndex(x,y,size)];

    for(int i = 0; i< size * size ; ++i){
        cout << b[i] << " ";
share|improve this answer

You can either swap the values in the matrix or swap the interpretation in the later functions.

For example, instead of printing a(i, j) you can print a(j,I) and print the trans pose.

That being said, what exaclty are you trying to do? If you look at LAPACK and BLAS, their routines take flags that control the algorithms to interpret them normally or as transposed.

share|improve this answer

without using swap function. len is the length of the array.

int i,j;
  N = sqrt(len);    
  int temp[len];
    {   for(j=0;j<N;j++)
            temp[j+(i*N)] = a[(j*N)+i];
share|improve this answer
static unsigned other(unsigned dim0, unsigned dim1, unsigned index)

#if 0
unsigned x0,x1;
x0 = index % dim0 ;
x1 = index / dim0 ;

return x0 * dim1 + x1;


unsigned mod,val;

mod = dim0 * dim1 -1;
val = (index==mod) ? mod: (dim1*index) % mod;
return val;

The above function returns the "other" index of index (the one in the transposed matrix := with x and y swapped). dim0 and dim1 are the "horizontal" and "vertical" size of the matrix. The #ifdeffed-out part is the naive implementation. In your case, you could initialise (or analyse) the 1-dimensional array with:

    arr[i] = other(3, 3, i);
share|improve this answer

Your Answer


