In Python I can reshape array remaining one dimension unknown, allowing Python to infer it automatically:
A=np.array([1,2,3,4,5,6])
np.reshape(A, (2,-1))
array([[1, 2, 3],
[4, 5, 6]])
Is the same possible in Mathematica?
UPDATE
In Python I can set any dimension to -1
, not only last, for example
A = np.array(range(3*4*5))
np.reshape(A, (3, -1, 5))
apparently, reshape
function calculates product of explicit dimensions and divides total length by it to compute unknown dimension. In Mathematica I can do this explicitly
A = Range[3*4*5]
ArrayReshape[A, {3, Length[A]/3/5, 5}]
but can I do as in Python?
EDIT: I reckon a better way to pose this question is to ask:
How can I implement similar functionality in Mathematica, preferably in a general way?
Partition
. $\endgroup$a = Range[6]; Partition[a, 3]
will give you your output.Partition
also has a lot of arguments you can play around with to generate more complex lists from your input. $\endgroup$Partition
? $\endgroup$