OKPEDIA PYTHON MATRIX

How to make a matrix in python

To make a matrix in python we use the array function of the numpy library.

import numpy as np
np.array (x)

Where x is the matrix to be created.

The function defines a matrix or vector object.

Example

This is a 2x2 matrix

np.array ([[1,2], [3,4]])

The elements of the row are separated by commas.

The rows of the array are enclosed in square brackets [ ] and separated by commas.

the structure of a python matrix

All rows in the array are enclosed in square brackets [ ].

Note. The array function can create vectors, matrices with two dimensions (3x3) or more dimensions (eg 3x3x3).

Other practical examples

Example 1 (3x2 matrix)

This command makes a rectangular 3x2 matrix

array ([[1,2], [3,4], [5,6]])

Example 2 (3x3 matrix)

This command makes a 3x3 square matrix

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

https://how.okpedia.org/en/python/how-to-make-a-matrix-in-python


Report an error or share a suggestion to enhance this page


Matrix in Python


FacebookTwitterLinkedinLinkedin