Skip to content Skip to sidebar Skip to footer

How To Use Arrays To Access Matrix Elements?

I need to change all nans of a matrix to a different value. I can easily get the nan positions using argwhere, but then I am not sure how to access those positions programmatically

Solution 1:

The more-efficient way of generating your output has already been covered by sacul. However, you're incorrectly indexing your 2D matrix in the case where you want to use an array.

At least to me, it's a bit unintuitive, but you need to use:

myMatrix[[all_row_indices], [all_column_indices]] 

The following will give you what you expect:

import numpy as np

myMatrix = np.array([[3.2,2,float('NaN'),3],[3,1,2,float('NaN')],[3,3,3,3]])

nanPositions = np.argwhere(np.isnan(myMatrix))
maxVal = np.nanmax(abs(myMatrix))

print(myMatrix[nanPositions[:, 0], nanPositions[:, 1]])

You can see more about advanced indexing in the documentation


Solution 2:

In [54]: arr = np.array([[3.2,2,float('NaN'),3],[3,1,2,float('NaN')],[3,3,3,3]])
    ...: 
In [55]: arr
Out[55]: 
array([[3.2, 2. , nan, 3. ],
       [3. , 1. , 2. , nan],
       [3. , 3. , 3. , 3. ]])

Location of the nan:

In [56]: np.where(np.isnan(arr))
Out[56]: (array([0, 1]), array([2, 3]))
In [57]: np.argwhere(np.isnan(arr))
Out[57]: 
array([[0, 2],
       [1, 3]])

where produces a tuple of arrays; argwhere the same values but as a 2d array

In [58]: arr[Out[56]]
Out[58]: array([nan, nan])
In [59]: arr[Out[56]] = [100,200]
In [60]: arr
Out[60]: 
array([[  3.2,   2. , 100. ,   3. ],
       [  3. ,   1. ,   2. , 200. ],
       [  3. ,   3. ,   3. ,   3. ]])

The argwhere can be used to index individual items:

In [72]: for ij in Out[57]:
    ...:     print(arr[tuple(ij)]) 
100.0
200.0

The tuple() is needed here because np.array([1,3]) in interpreted as 2 element indexing on the first dimension.

Another way to get that indexing tuple is to use unpacking:

In [74]: [arr[i,j] for i,j in Out[57]]
Out[74]: [100.0, 200.0]

So while argparse looks useful, it is trickier to use than plain where.

You could, as noted in the other answers, use boolean indexing (I've already modified arr so the isnan test no longer works):

In [75]: arr[arr>10]
Out[75]: array([100., 200.])

More on indexing with a list or array, and indexing with a tuple:

In [77]: arr[[0,0]]      # two copies of row 0
Out[77]: 
array([[  3.2,   2. , 100. ,   3. ],
       [  3.2,   2. , 100. ,   3. ]])
In [78]: arr[(0,0)]      # one element
Out[78]: 3.2

In [79]: arr[np.array([0,0])]         # same as list
Out[79]: 
array([[  3.2,   2. , 100. ,   3. ],
       [  3.2,   2. , 100. ,   3. ]])
In [80]: arr[np.array([0,0]),:]       # making the trailing : explicit
Out[80]: 
array([[  3.2,   2. , 100. ,   3. ],
       [  3.2,   2. , 100. ,   3. ]])

Solution 3:

You can do this instead (IIUC):

myMatrix[np.isnan(myMatrix)] = np.nanmax(abs(myMatrix))

Post a Comment for "How To Use Arrays To Access Matrix Elements?"