简体   繁体   中英

Do some mathematical operation on numpy array

I have a one numpy array look like:

 [array([580, 201]), array([167, 701]), array([167, 694]), array([979, 725]), array([ 200, 1271]), array([1005, 1266]), array([ 180, 1568]), array([ 994, 1591]), array([ 539, 1862])]

and I want to extract 580 from and 201 so that I can found min and max of x and y. So that I compare other coordinates with x_min,x_max,y_min and y_max and if the coordinates are lies in range so they will append otherwise we create new array.

I tried to find extract array element but then I can't find the any solution to do operation on it.


coordinates = [array([580, 201]), array([167, 701]), array([167, 694]), array([979, 725]), array([ 200, 1271]), array([1005, 1266]), array([ 180, 1568]), array([ 994, 1591]), array([ 539, 1862])]

for i in coordinates_new[0]:
    print(i)


output: 580 
201

I tried to do extract only 580 and 201 so that I compared to this with other coordinates when I use i[0] it will give error that IndexError: invalid index and scalar variable

I'm not sure I understood the question, but just to get things started:

import numpy as np
aa =[[580, 201], [167, 701], [167, 694], [979, 725], [ 200, 1271], [1005, 1266], [ 180, 1568], 
     [ 994, 1591], [ 539, 1862]]
print(f' list :\n{aa}\n')

bb=np.array(aa)
print(f' np.array :\n {bb}\n')

cc=np.transpose(bb)
print(f' transposed array :\n {cc}\n')

print(f' xmax: {np.max(cc[0])}')
print(f' ymax: {np.max(cc[1])}')
print(f' xmin: {np.min(cc[0])}')
print(f' ymin: {np.min(cc[1])}')

output:

list :
[[580, 201], [167, 701], [167, 694], [979, 725], [200, 1271], [1005, 1266], [180, 1568], [994, 1591], [539, 1862]]

 np.array :
 [[ 580  201]
 [ 167  701]
 [ 167  694]
 [ 979  725]
 [ 200 1271]
 [1005 1266]
 [ 180 1568]
 [ 994 1591]
 [ 539 1862]]

 transposed array :
 [[ 580  167  167  979  200 1005  180  994  539]
 [ 201  701  694  725 1271 1266 1568 1591 1862]]

 xmax: 1005
 ymax: 1862
 xmin: 167
 ymin: 201

applying some condition to elements of the array:

bb=[]  # empty list
for a in aa:
    print(a)
    if a[0]>200: bb.append(a)   # you can have two conditions, of course, a[1]>..
print()
for b in bb: 
    print(b) 

output:

[580, 201]
[167, 701]
[167, 694]
[979, 725]
[200, 1271]
[1005, 1266]
[180, 1568]
[994, 1591]
[539, 1862]

[580, 201]
[979, 725]
[1005, 1266]
[994, 1591]
[539, 1862]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM