1
$\begingroup$

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 this kind of image here, 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 this, which has the extra dimension that I don't want.

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

2 Answers 2

1
$\begingroup$
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)$

$\endgroup$
4
  • $\begingroup$ Thank you, this works perfectly. May you explain why it works? How is it different to what I attempted? $\endgroup$
    – Brad
    Jan 23, 2019 at 21:56
  • $\begingroup$ @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) $\endgroup$
    – kglr
    Jan 23, 2019 at 22:01
  • $\begingroup$ 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. $\endgroup$
    – Brad
    Jan 23, 2019 at 22:10
  • $\begingroup$ @Brad, my pleasure. Thank you for the accept. $\endgroup$
    – kglr
    Jan 23, 2019 at 22:11
2
$\begingroup$
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.

$\endgroup$
2
  • $\begingroup$ 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? $\endgroup$
    – Brad
    Jan 23, 2019 at 21:54
  • $\begingroup$ Also, may you elaborate about the nx1 matrices vs n-vectors? $\endgroup$
    – Brad
    Jan 23, 2019 at 21:55

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.