AppDividend
Latest Code Tutorials

# Python NumPy asmatrix() Function Example

Python numpy asmatrix() is an inbuilt numpy function that creates a matrix interpreting the given input. Unlike matrix function, it does not make a copy of the input provided is a matrix or ndarray. The numpy.asmatrix(data, dtype = None) returns a matrix by interpreting the input as a matrix.

## Python NumPy asmatrix()

NumPy asmatrix() function is used to interpret a given input as a matrix. Unlike matrix, asmatrix does not make a copy, if the input is already a matrix or a ndarray. Equivalent to matrix(data, copy=False).

### Syntax

`numpy.asmatrix(data, dtype=None)`

data: (array_like)

We are providing the input in the form of an array to convert that in the matrix.

dtype:(data-type)

The data type of the resultant matrix, which has been formed via asmatrix function, is provided by default, it’s the same as the inputted data.

### Return Value

It takes the inputted array and converts that into a matrix. It doesn’t make a copy if a matrix or ndarray is passed to it. The data type of the output matrix is the same as the inputted array.

### Example

```# app.py

import numpy as np

x = np.array([[1, 2], [3, 4]])
m = np.asmatrix(x)
print(m)
print('Manipulating the matrix formed via asmatrix function')
x[0, 0] = 5
print(m)
a = np.asmatrix(np.zeros((3, 1)))
print(a)```

#### Output

```python3 app.py
[[1 2]
[3 4]]
Manipulating the matrix formed via asmatrix function
[[5 2]
[3 4]]
[[0.]
[0.]
[0.]]```

## Programs on asmatrix() function in Python

Write a program to show the convert an array into the matrix and modify an element also in Python.

```# app.py

import numpy as np

data = []

rm = input("Enter the rows:")
cm = input("Enter the cols:")

row = int(rm)
col = int(cm)

print("Enter the data rowise:")
for i in range(row):
for j in range(col):
data.append(int(input()))

a = np.array(data).reshape(row, col)
print("as array: \n", a)

c = np.asmatrix(a)

print("as matrix: \n", c)

cn = input("Enter the row of modified value:")
cm = input("Enter the col of modified value:")
cb = input("Enter the value to be modified in the matrix:")

n = int(cn)
m = int(cm)
b = int(cb)

a[n - 1, m - 1] = b

print("as matrix after modifying value: \n", c)
```

#### Output

```python3 app.py
Enter the rows:2
Enter the cols:2
Enter the data rowise:
2
2
2
2
as array:
[[2 2]
[2 2]]
as matrix:
[[2 2]
[2 2]]
Enter the row of modified value:1
Enter the col of modified value:1
Enter the value to be modified in the matrix:3
<class 'numpy.ndarray'>
as matrix after modifying value:
[[3 2]
[2 2]]```

Let’s see other output variations.

```Value for parameter a=array([[1, 2],[3, 4]])

Enter the rows:2
Enter the cols:2
Enter the data rowise:
1
2
3
4
as array: array([[1, 2],
[3, 4]]))

as matrix: matrix([[1, 2],
[3, 4]]))

Enter the row of modified value:1
Enter the col of modified value:1
Enter the value to be modified in the matrix:10

As matrix after modifying value: matrix([[10, 2],
[3, 4]]))
Value for parameter a=array([[5, 6, 8, 4],[9, 7, 3, 1]])

Enter the rows:2
Enter the cols:4
Enter the data rowise:
5
6
8
4
9
7
3
1

as array: array([[5, 6, 8, 4],
[9, 7, 3, 1]]))

as matrix: matrix([[5, 6, 8, 4],
[9, 7, 3, 1]]))
Enter the row of modified value:2
Enter the col of modified value:3
Enter the value to be modified in the matrix:2

As matrix after modifying value: matrix([[5, 6, 8, 4],
[9, 7, 2, 1]]))

Value for parameter a=array([,])

Enter the rows:1
Enter the cols:1
Enter the data rowise:
11
10

as array: array([,
]))

as matrix: matrix([,
]))
Enter the row of modified value:1
Enter the col of modified value:1
Enter the value to be modified in the matrix:8

As matrix after modifying value: matrix([,
]))
Value for parameter a=array([[15, 16, 18],[19, 17, 20],[11, 12, 13])

Enter the rows:3
Enter the cols:3
Enter the data rowise:
15
16
18
19
17
20
11
12
13
as array: array([[15, 16, 18],
[19, 17, 20],
[11, 12, 13]]))

as matrix: matrix([[15, 16, 18],
[19, 17, 20],
[11, 12, 13]]))
Enter the row of modified value:3
Enter the col of modified value:3
Enter the value to be modified in the matrix:50

As matrix after modifying value: matrix([[15, 16, 18],
[19, 17, 20],
[11, 12, 50]]))
```

Here the data for the matrix has been provided by the user, and the inputted data is converted into the matrix.

The user is providing the data for the matrix as well as the size of the matrix too. This does not make a copy like a matrix function.

After converting the input data into the matrix, we are modifying the value at place inputted by the user with the user inputted value.

## Conclusion

The asmatrix() function interprets the input as a matrix. It is not the same as a matrix because asmatrix does not make a copy if the input is already a matrix or a ndarray. Equivalent to matrix(data, copy=False).