0

座標を表す、これらに似た numpy 配列が 2 つあります。

import numpy as np
x=np.array([1,3,2,4,6,5,4,1])
y=np.array([4,4,3,2,2,1,3,5])

私もn正方形を持っています:

s1 -> x=0 to 3, y=0 to 3
s2 -> x=3 to 6, y=3 to 6
s3 -> ...
s4 -> ...

そして、各正方形に収まる点の数を数えたいと思います。nこれは、不等式の評価に帰着します。

私のアプローチは冗長で (おそらく) 非効率的です。

count1=0
count2=0
count3=0
count4=0
for j in range(0, len(x)):
    #Square 1
    if x[j]<=3 and y[j]<=3:
        count1+=1
    #Square 2
    if x[j]<=3 and y[j]>3 and y[j]<=6:
        count2+=1
    #Square 3
    if x[j]>3 and x[j]<=6 and y[j]<=3:
        count3+=1
    #Square 4
    if x[j]>3 and x[j]<=6 and y[j]>3 and y[j]<=6:
        count4+=1

2 つの配列を指定すると、次のように返されます。

In[1]: count1, count2, count3, count4
Out[1]: (1, 3, 4, 0)

私の実際の問題は、さまざまな数の正方形で構成されています (6、36 など)。

変数の生成と、ステートメントcountの数と境界の両方を自動化する方法はありますか?if

4

2 に答える 2