4
$\begingroup$

I have large number of columns of data. How to put in single matrix?

For example, with test data of 1 x 3 matrixs

test[1] = {1, 1, 1}
test[2] = {2, 2, 2}
test[3] = {3, 3, 3}
test[4] = {4, 4, 4}

I use

Array[test, 4, 1, Join]

and get 1 x 12 matrix

{1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4}

I want to get 4 x 3 matrix

{{1, 2, 3, 4}, {1, 2, 3, 4}, {1, 2, 3, 4}}
$\endgroup$
1
  • 1
    $\begingroup$ Don't you already have a 4 x 3 matrix or let's say 2-dimensional array? $\endgroup$ Commented Jan 23, 2019 at 19:11

5 Answers 5

7
$\begingroup$

You can try creating an Array and then Transposing:

In[14]:= Transpose @ Array[test, 4]

Out[14]= {{1, 2, 3, 4}, {1, 2, 3, 4}, {1, 2, 3, 4}}
$\endgroup$
6
$\begingroup$
num = 4;
test[#] & /@ Range[num] // Transpose

{{1, 2, 3, 4}, {1, 2, 3, 4}, {1, 2, 3, 4}}

$\endgroup$
1
  • 4
    $\begingroup$ test /@ Range... will do too. $\endgroup$
    – Kuba
    Commented Jan 23, 2019 at 11:38
5
$\begingroup$
Array[test, 4, 1, Transpose[{##}] &]

{{1, 2, 3, 4}, {1, 2, 3, 4}, {1, 2, 3, 4}}

also

Array[test, 4, 1, Flatten[{##}, {2}] &]
Array[test, 4, 1, Thread[{##}] &]
$\endgroup$
3
$\begingroup$

Another possibility:

Array[List /@ test[#] &, 4, 1, Join[##, 2] &]
$\endgroup$
0
$\begingroup$

I hope this helps in creating the array you're asking about.

Var matrix = new int [4, 3];   
     //this creates an array (of arrays) with 4 rows, 3 columns in each row

IF THE VALUES ARE KNOWN (AS IN YOUR CASE) = 
Var matrix = new int [4, 3];
{
    {1, 1, 1}
    {2, 2, 2}
    {3, 3, 3}
    {4, 4, 4}
}
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.