Liszt
Liszt

Reputation: 157

Transpose (treating the array) a one dimensional array in java

if i have the following table:

int v[] = new int [2];
v[0]=20;
v[1]=35;

so i get

v= [20 35]

Now, i want to transpose the table v in order to obtain:

v= [20

    35

    ]

but how ?

Best Regards,

Liszt.

Upvotes: 0

Views: 903

Answers (3)

Alex
Alex

Reputation: 11579

Try this:

double[] arr = new double[] {20, 35};
RealMatrix rm = new Array2DRowRealMatrix(arr);
rm.transpose();

Upvotes: 1

andy256
andy256

Reputation: 2881

int v[] = new int [2];
v[0]=20;
v[1]=35;

If you transpose this table (in this case it is a vector), you end up with the same values in the same places. See this Wikipedia article. As your own diagram shows, it is how you visualize it and what you take the index to represent.

If you hold your vector in a square matrix, transposition would be more obvious:

a b
c d

transposes to

a c
b d

Upvotes: 2

morgano
morgano

Reputation: 17422

If you're treating the array as a matrix of 1xN, then just create an array per element:

int v[] = new int [2];
v[0]=20;
v[1]=35;

// ...

int[][] vT = new int[v.length][1];
for(int i = 0; i < v.length; i++) {
    vT[i][0] = v[i];
}

Upvotes: 1

Related Questions