0

I would like to "concatenate" results of detections of positions of specific values in my array "Coord3".

I have a double criteria on my array for 2 specific dimension of it. I get some redondant positions and i would like to gather it in order to apply a mask of this values.

As example with a moderate array :

import numpy as np

Coord3 = np.array([[[[ 0.,  0.],
        [ 0.,  1.],
        [ 0.,  2.]],

       [[ 1.,  0.],
        [ 1.,  1.],
        [ 1.,  2.]],

       [[ 2.,  0.],
        [ 2.,  1.],
        [ 2.,  2.]]],


        [[[ 1.,  0.],
        [ 1.,  1.],
        [ 1.,  2.]],

       [[ 2.,  0.],
        [ 2.,  1.],
        [ 2.,  2.]],

       [[ 4.,  0.],
        [ 3.,  1.],
        [ 4.,  2.]]],


        [[[ 2.,  0.],
        [ 2.,  1.],
        [ 2.,  2.]],

       [[ 3.,  0.],
        [ 3.,  1.],
        [ 3.,  2.]],

       [[ 4.,  0.],
        [ 4.,  1.],
        [ 4.,  4.]]]])


#I apply my double criteria in 2 shapes of my array Coord3

plaY=[]

for i in range(Coord3.shape[0]):
    holding_list = zip(*np.where(Coord3[i,:,:,0] > 3))
    plaY.append(holding_list)

plaY_array = np.asarray(plaY)

#plaY_array
#Out[1088]: array([[], [(2, 0), (2, 2)], [(2, 0), (2, 1), (2, 2)]], dtype=object)



plaX=[]

for i in range(Coord3.shape[0]):
    holding_list = zip(*np.where(Coord3[i,:,:,1] > 2))
    plaX.append(holding_list)

plaX_array = np.asarray(plaX)


#plaX_array
#Out[1097]: array([[], [], [(2, 2)]], dtype=object)

So (2,0) and (2,2) are redondant and i would like to supress it and to gather all in an unique array... as :

np.array([[2, 0], [2, 2],....])

--- EDIT LATER ------------------------------------------

I could concatenate for each specific time

plaY_array = plaY_array[:,np.newaxis]

plaX_array = plaX_array[:,np.newaxis]


test = plaX_array + plaY_array

#I get that :

#test
#array([[[[]]],

       #[[[(2, 0), (2, 2)]]],

       #[[[(2, 0), (2, 1), (2, 2), (2, 2)]]]], dtype=object)

So now i have to suppress just the (2,2) redondant in the "third slice", it could be interesting to know the coordinates for each specific time (so i let the (2,2) in "2nd slice"

1 Answer 1

1

You could turn the list of coordinates into a set to remove duplicates:

In [21]: set(zip(*(np.where(Coord3[:,:,:,0] > 3)[1:])))
Out[21]: {(2, 0), (2, 1), (2, 2)}

plaY_array = np.array(list(set(zip(*(np.where(Coord3[:,:,:,0] > 3)[1:])))))
plaX_array = np.array(list(set(zip(*(np.where(Coord3[:,:,:,1] > 2)[1:])))))    

print(plaY_array)
# [[2 0]
#  [2 1]
#  [2 2]]    

print(plaX_array)
# [[2 2]]

Also note that you can eliminate the for-loop

for i in range(Coord3.shape[0]):

by calling np.where(Coord3[:,:,:,0] > 3) instead of np.where(Coord3[i,:,:,0] > 3) for each i:

In [16]: np.where(Coord3[:,:,:,0] > 3)
Out[16]: (array([1, 1, 2, 2, 2]), array([2, 2, 2, 2, 2]), array([0, 2, 0, 1, 2]))

The i values are in the first array, but since you don't care about those, you can just drop the first array.

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks for the answer! ;) as i have just said, it could be interesting to consider the different frames. I consider 3 times and i would to stock the results as an array of shape (3,2) with 3 for times and 2 for the positions. Sorry to bring later conditions...
My difficulty is due to the special shape of the "test array"(3, 1, 1). I dont know how to get acess to coordinates separately...
I'm confused. What are the extra conditions and what is the desired final result?
Sorry i m not clear! :s as you can see in my second part of the edit, i got a "test array" which represents the couple of coordinates satisfactory the 2 conditions >3 and >2, but, in this array, i got : [[[(2, 0), (2, 1), (2, 2), (2, 2)]]]], dtype=object) in third line, (2,2) is redondant i would like to suppress it
and I would like to reshape the results in an array whose shape is (3,2) in order to get access easily to my results (because with the shape (3, 1, 1), i dont manage to access to values).But perhaps for this part, i should ask in another question...
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.