NumPy | asmatrix method
asmatrix(~) method constructs a matrix from a sequence of data (e.g. arrays and tuples).
The sequence of data used to construct a matrix.
a is a NumPy array or matrix, no copying would be done - modifying the result of
asmatrix(~) would also automatically modify
Type of data stored in the NumPy array. By default, the type will be inferred.
A NumPy matrix.
To create a 2 by 2 matrix:
np.asmatrix([[4,5],[6,7]])matrix([[4, 5],[6, 7]])
Case when array is not copied
Suppose we create a NumPy matrix out of a NumPy array, like so:
When we modify the contents of
b[0,0] = 9bmatrix([[9, 5],[6, 7]])
This would automatically modify
a as well:
aarray([[9, 5],[6, 7]])
What's happening here is that the NumPy objects
b actually share the same memory space, so modifying one would involve modifying the other. Note that this happens only if we try to call
asmatrix(~) on a NumPy array or matrix.