# Joining $4 \times 1$ vectors defined through functions to form a $4 \times n$ matrix

I define a $$4x1$$column vector through a variable as:

Z[i_]:=List[{a[i]},{b[i]},{c[i]},{d[i]}]


I then want to make a $$4\times n$$ matrix by joining a specified number of these columns together. By other posts on StackExchange, I've found that by using Join[Z[1],Z[2],Z[3],2], and transposing as needed, I can get the form I desire, except that this has dimensions of $$4\times 4\times 1$$, with an extra List around each of the terms in the matrix.

How can I combine columns as one would normally expect by writing them down? Is there a way to automate this where I can specify how many columns I want to combine, i.e to make a specified $$4\times n$$ matrix rather than 'manually' combining my desired dimension?

EDIT: The desired form of the matrix (through the definition of the Z function is , a $$4\times 4$$ matrix. The result I get using the Join function with an attempt at automation (which I could extend with the Range function is , which has the extra dimension that I don't want.

• what is the desired output for n =3? Join[Z[1],Z[2],Z[3],2] does give a 4X3 matrix.
– kglr
Jan 23, 2019 at 21:45
• As an example for n=4 which is my lowest case, I will upload the desired result as an edit.
Jan 23, 2019 at 21:47

n = 4;
Join[## & @@ Z /@ Range[n], 2]


{{a[1], a[2], a[3], a[4]}, {b[1], b[2], b[3], b[4]}, {c[1], c[2], c[3], c[4]}, {d[1], d[2], d[3], d[4]}}

TeXForm@MatrixForm@%


$$\left( \begin{array}{cccc} a(1) & a(2) & a(3) & a(4) \\ b(1) & b(2) & b(3) & b(4) \\ c(1) & c(2) & c(3) & c(4) \\ d(1) & d(2) & d(3) & d(4) \\ \end{array} \right)$$

• Thank you, this works perfectly. May you explain why it works? How is it different to what I attempted?
Jan 23, 2019 at 21:56
• @Brad, Join takes a Sequence of lists followed by an integer (specifying the level) . ##&@@{a,b,c} turns the list {a,b,c} too Sequence[a,b,c] which is what Join needs. (Btw, ##&@@{a,b,c} is same as Sequence@@{a,b,c}. And foo@@bar[stuff] gives foo[stuff] , i.e., it replaces the Head (bar) with foo)
– kglr
Jan 23, 2019 at 22:01
• Thank you for your help. I've seen these # and & symbols thrown around a lot, I'll go do some more research as to what these actually do (I think it's something to do with Slots?). I've accepted your answer. Best wishes.
Jan 23, 2019 at 22:10
• @Brad, my pleasure. Thank you for the accept.
– kglr
Jan 23, 2019 at 22:11
Z[i_] := List[{a[i]}, {b[i]}, {c[i]}, {d[i]}]
Transpose[Flatten@*Z /@ Range[5]]


or simpler:

Z[i_] := {a[i], b[i], c[i], d[i]}
Transpose[Z /@ Range[5]]


You are going to make yourself unhappy if you stick to using $$n \times 1$$ matrices instead of $$n$$-vectors.

• Thank you for your reply. Your second answers gives me the same problem as before, but the first one works like a charm. I tried using Flatten, but can you explain why your one works? What does each component do?