Mathematica Construct matrix from vectors in Mathematica

Click For Summary
To construct a matrix in Mathematica using predefined column vectors, the Transpose function is effective. For example, defining vectors as v1={a,b} and v2={c,d} allows the creation of the desired matrix with mat=Transpose[{v1,v2}], resulting in {{a,c},{b,d}}. However, if vectors are defined as v1={{a},{b}} and v2={{c},{d}}, using Transpose directly will not yield the intended matrix format. Instead, applying mat=Transpose[{Flatten[v1],Flatten[v2]}] successfully converts the nested vectors into the correct matrix format, producing {{a,c},{b,d}}. This approach clarifies how Mathematica handles vector dimensions and ensures the correct matrix structure.
nikolafmf
Messages
112
Reaction score
0
I have defined vectors which are to be column vectors of some matrix. How to tell Mathematica to construct such matrix whose column vectors are already defined vectors?
 
Physics news on Phys.org
Mathematica doesn't usually distinguish row from column vectors and will use a vector as needed.

So if you have

In[1]:= v1={a,b};v2={c,d};

Then this will give you what you want

In[3]:= mat=Transpose[{v1,v2}]
Out[3]= {{a,c},{b,d}}

But if you have done something like this, thinking you need that for columns,

In[4]:= v1={{a},{b}};v2={{c},{d}};

then this will undo what you have done and give you what you want

In[6]:= mat=Transpose[{Flatten[v1],Flatten[v2]}]
Out[6]= {{a,c},{b,d}}
 
It works! Thank you very much!
 

Similar threads

  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 11 ·
Replies
11
Views
2K
Replies
2
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K