Khashayar Ghamati
Khashayar Ghamati

Reputation: 368

how can I insert an element to Numpy matrix by row and column indices

I want to create a matrix with Numpy, but I need to add every element by its row and column indices.

for example:

my_matrix = np.matrix(np.zeros((5, 5))) 
my_matrix.insert(row_index=2, column_index=1, value=10)

output:
matrix([[0., 0., 0., 0., 0.],
    [10., 0., 0., 0., 0.],
    [0., 0., 0., 0., 0.],
    [0., 0., 0., 0., 0.],
    [0., 0., 0., 0., 0.]])

How can I do that?

Upvotes: 1

Views: 1407

Answers (2)

hpaulj
hpaulj

Reputation: 231335

The use of np.matrix is discouraged, if not actually deprecated. It is rarely needed, except for some backward compatibility cases.

In [1]: arr = np.zeros((5,5))                                                                    
In [2]: arr                                                                                      
Out[2]: 
array([[0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0.]])
In [3]: mat = np.matrix(arr)                                                                     
In [4]: mat                                                                                      
Out[4]: 
matrix([[0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0.]])

Indexing one row of arr produces a 1d array

In [5]: arr[2]                                                                                   
Out[5]: array([0., 0., 0., 0., 0.])

Indexing one row of mat produces a 2d matrix, with shape (1,5)

In [6]: mat[2]                                                                                   
Out[6]: matrix([[0., 0., 0., 0., 0.]])

We can access an element in the 1d array:

In [7]: arr[2][1]                                                                                
Out[7]: 0.0

but this indexing of the mat tries to access a row, and gives an error:

In [8]: mat[2][1]                                                                                
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-8-212ad5378f8e> in <module>
----> 1 mat[2][1]
 ...
IndexError: index 1 is out of bounds for axis 0 with size 1

In both cases it is better to access an element with the tuple syntax, rather than the chained one:

In [9]: arr[2,1]                                                                                 
Out[9]: 0.0
In [10]: mat[2,1]                                                                                
Out[10]: 0.0

This indexing also works for setting values. But try to avoid iterating to set individual values. Try to find ways of creating the whole array with the desired values directly, with whole array methods, not iteration.

Upvotes: 1

Do you want to add or insert values? The add function that you mentioned is used as an element-wise addition.

Example:

np.add([1, 2], [2, 3])
Out[41]: array([3, 5])

If you really want to create a matrix a matrix by inserting values with column and row indices, create the matrix first and insert your values afterwards.

number_rows = 10
number_cols = 20
arr = np.empty((number_rows, number_cols))
arr[2, 1] = 10

Upvotes: 1

Related Questions