- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Interpret the input as a matrix in Numpy

To Interpret the input as a matrix, use the **numpy.asmatrix()** method in Python Numpy. Unlike matrix, asmatrix does not make a copy if the input is already a matrix or an ndarray. Equivalent to matrix(data, copy=False).

NumPy offers comprehensive mathematical functions, random number generators, linear algebra routines, Fourier transforms, and more. It supports a wide range of hardware and computing platforms, and plays well with distributed, GPU, and sparse array libraries.

## Steps

At first, import the required library −

import numpy as np

Create a 2d array −

arr = np.array([[36, 36, 78, 88], [92, 81, 98, 45], [22, 67, 54, 69 ], [69, 80, 80, 99]])

Displaying our array −

print("Array...\n",arr)

Get the datatype −

print("\nArray datatype...\n",arr.dtype)

Get the dimensions of the Arra −

print("\nArray Dimensions...\n",arr.ndim)

Get the shape of the Array −

print("\nOur Array Shape...\n",arr.shape)

Get the number of elements of the Array −

print("\nElements in the Array...\n",arr.size)

To Interpret the input as a matrix, use the numpy.asmatrix() method in Python Numpy −

res = np.asmatrix(arr) print("\nResult...\n",res)

Set some values −

arr[0,2] = 99 arr[1,1] = 199 arr[2,1] = 299

Display the updated matrix −

print("\nUpdated Matrix...\n",arr)

## Example

import numpy as np # Create a 2d array arr = np.array([[36, 36, 78, 88], [92, 81, 98, 45], [22, 67, 54, 69], [69, 80, 80, 99]]) # Displaying our array print("Array...\n",arr) # Get the datatype print("\nArray datatype...\n",arr.dtype) # Get the dimensions of the Array print("\nArray Dimensions...\n",arr.ndim) # Get the shape of the Array print("\nOur Array Shape...\n",arr.shape) # Get the number of elements of the Array print("\nElements in the Array...\n",arr.size) # To Interpret the input as a matrix, use the numpy.asmatrix() method in Python Numpy res = np.asmatrix(arr) print("\nResult...\n",res) # Set some values arr[0,2] = 99 arr[1,1] = 199 arr[2,1] = 299 # Display the updated matrix print("\nUpdated Matrix...\n",arr)

## Output

Array... [[36 36 78 88] [92 81 98 45] [22 67 54 69] [69 80 80 99]] Array datatype... int64 Array Dimensions... 2 Our Array Shape... (4, 4) Elements in the Array... 16 Result... [[36 36 78 88] [92 81 98 45] [22 67 54 69] [69 80 80 99]] Updated Matrix... [[ 36 36 99 88] [ 92 199 98 45] [ 22 299 54 69] [ 69 80 80 99]]

- Related Questions & Answers
- Interpret the input as a matrix and display a different type for the output in Numpy
- Create a two-dimensional array with the flattened input as a diagonal in Numpy
- Create a two-dimensional array with the flattened input as lower diagonal in Numpy
- How can MySQL interpret the number and string, having no delimiter, as a date?
- Create a two-dimensional array with the flattened input as an upper diagonal in Numpy
- Build a block matrix in Numpy
- Generate a Vandermonde matrix in Numpy
- Return the floor of the input in Numpy
- Convert the input to a masked array conserving subclasses in Numpy
- How to save matrix created in R as tables in a text file with column names same as the matrix?
- Return the pickle of the masked array as a string in NumPy
- A square matrix as sum of symmetric and skew-symmetric matrix ?
- Return the underlying data as a view of the masked array in Numpy
- Python Get a list as input from user
- Convert the input to a masked array of the given data-type in Numpy