2.21 Convert N by M matrix to a row of length N M

Given

a=[1 2 3 
   4 5 6 
   7 8 9]
 

covert the matrix to one vector

[1 2 3 4 5 6 7 8 9]
 

Mathematica

a={{1,2,3}, 
   {4,5,6}, 
   {7,8,9}} 
 
Flatten[a]
 

{1,2,3,4,5,6,7,8,9}
 

 

Matlab

a=[1 2 3; 
   4 5 6; 
   7 8 9] 
 
a=a(:); 
a'
 

 1 4 7 2 5 8 3 6 9
 

 

Maple Maple reshapes along columns, like Matlab. To get same result as Mathematica, we can transpose the matrix first. To get same result as Matlab, do not transpose.

A:=Matrix([[1,2,3],[4,5,6],[7,8,9]]); 
v:=ListTools:-Flatten(convert(A,list)); 
 
v:=ListTools:-Flatten(convert(A^%T,list));
 

[1, 4, 7, 2, 5, 8, 3, 6, 9] 
 
[1, 2, 3, 4, 5, 6, 7, 8, 9]