Home > Mobile >  Can I find an index from the minimum of one array and then use that same index to call a column from
Can I find an index from the minimum of one array and then use that same index to call a column from

Time:10-25

I'm trying to find the index of the minimum value of a 1D array and then find the corresponding column from a 2D array, I do this using:

find_index = np.where(min(function())

where the function creates the array in question. The array is a single column with 8 values. This seems to be working, but the problem arises when I then try to find the corresponding column of the 8x8 array. I've tried

find_column = varr[:,find_index]

and also

column_needed = [:,find_index]
find_column = np.take(varr, column_needed)

where varr is the 8x8 array and find_index is the index I found from the 1D array. Is there a way to do this? I think I understand why my approaches aren't working but I can't seem to find an approach that does work.

varr = np.array([1],[2],[3]) 
varr2 = np.array([1,2,3], [4,5,6], [7,8,9]) 
find_index = np.where(min(varr)) 
find_column = varr2[:,find_index]'

Edited to attempt to get the code to show up as code and add a simple example, this is my first post :)

CodePudding user response:

It was almost correct, the issue being that where returns a tuple.

You can use:

idx = varr.argmin(axis=0)

col = varr[:, idx]
print(col)

Fix of your code:

find_index = np.where(varr.min()) 
find_column = varr2[:,find_index[0]]

output:

array([[1],
       [2],
       [3]])

CodePudding user response:

This should work:

varr = np.array([1,2,3]) 
varr2 = np.array([[1,2,3], [4,5,6], [7,8,9]]) 
find_index = np.where(min(varr))[0]
find_column = varr2[:,find_index]  
  • Related