Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Transpose 1 Dimensional Array

Tags:

c++

c

algorithm

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];                                                                                                                                                                                                    
for ( int i = 0 ; i < 9 ; i ++ )
         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 ?

like image 547
Legolas Avatar asked Oct 02 '11 21:10

Legolas


People also ask

What is the transpose of a 1D array?

The transpose of a 1D array is still a 1D array! (If you're used to matlab, it fundamentally doesn't have a concept of a 1D array. Matlab's "1D" arrays are 2D.) If you want to turn your 1D vector into a 2D array and then transpose it, just slice it with np.

How do you transpose a one dimensional array in Python?

To transpose NumPy array ndarray (swap rows and columns), use the T attribute ( . T ), the ndarray method transpose() and the numpy. transpose() function.

How do you transpose a 1D array in Labview?

You can't transpose a 1D array (it only has one dimension!), but you can do what you want. You can use build array to combine the 3 vectors into 1 2D array, and then use Transpose Array on the 2D array.

How do you change 1D to 2D array?

Use reshape() Function to Transform 1d Array to 2d Array The number of components within every dimension defines the form of the array. We may add or delete parameters or adjust the number of items within every dimension by using reshaping.


1 Answers

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

for(int i = 0; i < n; ++i)
    for(int j = i+1; j < n; ++j)
        std::swap(Array[n*i + j], Array[n*j + i]);
like image 115
wxffles Avatar answered Sep 29 '22 12:09

wxffles