{"id":27535,"date":"2022-08-16T00:03:09","date_gmt":"2022-08-15T18:33:09","guid":{"rendered":"https:\/\/python-programs.com\/?p=27535"},"modified":"2022-08-16T00:03:09","modified_gmt":"2022-08-15T18:33:09","slug":"python-numpy-matrix-put-function","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-numpy-matrix-put-function\/","title":{"rendered":"Python Numpy matrix.put() Function"},"content":{"rendered":"
NumPy Library\u00a0<\/strong><\/p>\n NumPy is a library in python that is created to work efficiently with arrays in python. It is fast, easy to learn, and provides efficient storage. It also provides a better way of handling data for the process. We can create an n-dimensional array in NumPy. To use NumPy simply have to import it into our program and then we can easily use the functionality of NumPy in our program.<\/p>\n NumPy is a Python library that is frequently used for scientific and statistical analysis. NumPy arrays are grids of the same datatype\u2019s values.<\/p>\n Numpy matrix.put() Function:<\/strong><\/p>\n Using the matrix.put() method of the Numpy module, we can put(insert) the value by passing index and value in a given particular matrix.<\/p>\n Syntax:<\/strong><\/p>\n Return Value:<\/strong><\/p>\n A new matrix (after putting values ) is returned by the put() function.<\/p>\n For 2-Dimensional (2D) Matrix (Inserting an element at Multiple Indices)<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Explanation:<\/strong><\/p>\n NOTE:<\/strong><\/p>\n For 3-Dimensional (3D) Matrix (Inserting an element at Single Index)<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n <\/p>\n <\/p>\n","protected":false},"excerpt":{"rendered":" NumPy Library\u00a0 NumPy is a library in python that is created to work efficiently with arrays in python. It is fast, easy to learn, and provides efficient storage. It also provides a better way of handling data for the process. We can create an n-dimensional array in NumPy. To use NumPy simply have to import …<\/p>\n matrix.put(index, value)<\/pre>\n
Numpy matrix.put() Function in Python<\/h2>\n
\n
# Import numpy module using the import keyword\r\nimport numpy as np\r\n \r\n# Create a matrix(2-Dimensional) using the matrix() function of numpy module by passing \r\n# some random 2D matrix as arguments to it and store it in a variable\r\ngvn_matrx = np.matrix('[2, 1; 6, 3]')\r\n \r\n# Pass the index\/indices(as a tuple) and value as an argument to the put() function and apply it to the given matrix\r\n# Here we passed multiple indices and the value gets inserted at corresponding indices of the tuple.\r\n# Store it in another variable\r\ngvn_matrx.put((0, 2), 50)\r\n# Print the given matrix after inserting an element at the multiple indices\r\nprint(\"The given matrix after inserting an element at the multiple indices:\")\r\nprint(gvn_matrx)<\/pre>\n
The given matrix after inserting an element at the multiple indices:\r\n[[50 1]\r\n[50 3]]<\/pre>\n
Here it inserts 50 at the indices 0 and 2.<\/pre>\n
The matrix indices starts from '0'<\/pre>\n
\n
# Import numpy module using the import keyword\r\nimport numpy as np\r\n \r\n# Create a matrix(3-Dimensional) using the matrix() function of numpy module by passing \r\n# some random 3D matrix as an argument to it and store it in a variable\r\ngvn_matrx = np.matrix('[2, 4, 1; 8, 7, 3; 10, 9, 5]')\r\n \r\n# Pass the index and value as arguments to the put() function and apply it to the given matrix\r\n# Here we passed single index and the value gets inserted at that corresponding index.\r\n# Store it in another variable\r\ngvn_matrx.put((4), 100)\r\n# Print the given matrix after inserting an element at the given index.\r\nprint(\"The given matrix after inserting an element {100} at the given index{4}:\")\r\nprint(gvn_matrx)<\/pre>\n
The given matrix after inserting an element {100} at the given index{4}:\r\n[[ 2 4 1]\r\n [ 8 100 3]\r\n [ 10 9 5]]<\/pre>\n