溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

講解Python3中NumPy數(shù)組尋找特定元素下標(biāo)的兩種方法

發(fā)布時間:2020-08-24 16:48:40 來源:腳本之家 閱讀:410 作者:西門丫丫 欄目:開發(fā)技術(shù)

引子

Matlab中有一個函數(shù)叫做find,可以很方便地尋找數(shù)組內(nèi)特定元素的下標(biāo),即:Find indices and values of nonzero elements。
這個函數(shù)非常有用。比如,我們想計算圖1中點Q(x0, y0)拋物線的最短距離。一個可以實施的方法是:計算出拋物線上所有點到Q點的距離,找到最小值,用find函數(shù)找到最小值對應(yīng)的下標(biāo),即M點橫坐標(biāo)和縱坐標(biāo)對應(yīng)的元素的下標(biāo),M點到Q點的距離就是最短距離。

講解Python3中NumPy數(shù)組尋找特定元素下標(biāo)的兩種方法 

首先給出Matlab使用find函數(shù)實現(xiàn)的代碼:

a = linspace(-5,5,1000);
b = a .^2;
x0 = 4;
y0 = 4;
dis = sqrt((a - x0).^2 + (b - y0).^2);
mm = find (dis == min(dis));
a0 = a(mm);
b0 = b(mm);
disMin = sqrt((a0 - x0).^2 + (b0 - y0).^2);
plot(a, b);
hold on;
scatter(x0, y0, 'k*');
scatter(a0, b0, 'k*');
xx = [a0, x0];
yy = [b0, y0];
plot(xx, yy);

講解Python3中NumPy數(shù)組尋找特定元素下標(biāo)的兩種方法

NumPy中的where函數(shù)

Syntax: np.where(conditions, [x,y])

具體實現(xiàn)代碼如下:

import numpy as np
import math
import matplotlib.pyplot as plt

a = np.linspace(-5, 5, 10000)
b = a * a
x0 = 4
y0 =4
num = np.linspace(0, len(a) - 1, len(a))
dis = np.linspace(0, 0, len(a))
for k in num:
  k = int(k)
  dis[k] = dis[k] + math.sqrt((a[k] -x0) **2 + (b[k] - y0) **2)
disMin = min(dis)
disMinIndex = np.where(dis == disMin)
disMin0 = math.sqrt((a[disMinIndex] - x0) **2 + (b[disMinIndex] - y0) **2)
print('The mininum distance:',disMin)
print('The mininum distance:',disMin0)
print(type(dis))
a0 = a[disMinIndex]
b0 = b[disMinIndex]
fig = plt.figure(figsize = (6,6), dpi = 200)
ax1 = plt.subplot(1,1,1)
line11 = ax1.scatter(a,b,s = 1)
line12 = ax1.scatter(x0, y0, s = 100, marker = '*', color = 'darkorange')
line13 = ax1.scatter(a0, b0, s = 100, marker = '*', color = 'darkorange')
line14 = ax1.plot([x0,a0],[y0,b0], color = 'darkorange')
line15 = ax1.text(4.2,4,'Q(x0,y0)')
line16 = ax1.text(0.6,5, 'M(a0,b0)')
line18 = plt.xlim(-5,5)
line17 = plt.ylim(0,25)
plt.savefig('C:/Users/BRIAR/Desktop/index.png')
plt.show()

The mininum distance: 1.943317035
The mininum distance: 1.9433170350024023
class ‘numpy.ndarray'

講解Python3中NumPy數(shù)組尋找特定元素下標(biāo)的兩種方法

List中的index函數(shù)

Syntax: List.index(aimElement)
注意:此處需將NumPy數(shù)組轉(zhuǎn)換成List格式的數(shù)據(jù)。
具體實現(xiàn)代碼如下:

import numpy as np
import math
import matplotlib.pyplot as plt

a = np.linspace(-5, 5, 10000)
b = a * a
x0 = 4
y0 =4
num = np.linspace(0, len(a) - 1, len(a))
dis = np.linspace(0, 0, len(a))
for k in num:
  k = int(k)
  dis[k] = dis[k] + math.sqrt((a[k] -x0) **2 + (b[k] - y0) **2)
disMin = min(dis)
disList = dis.tolist()
disMinIndex = disList.index(disMin)
disMin0 = math.sqrt((a[disMinIndex] - x0) **2 + (b[disMinIndex] - y0) **2)
print('The mininum distance:',disMin)
print('The mininum distance:',disMin0)
print(type(disList))
a0 = a[disMinIndex]
b0 = b[disMinIndex]
fig = plt.figure(figsize = (6,6), dpi = 200)
ax1 = plt.subplot(1,1,1)
line11 = ax1.scatter(a,b,s = 1)
line12 = ax1.scatter(x0, y0, s = 100, marker = '*', color = 'darkorange')
line13 = ax1.scatter(a0, b0, s = 100, marker = '*', color = 'darkorange')
line14 = ax1.plot([x0,a0],[y0,b0], color = 'darkorange')
line15 = ax1.text(4.2,4,'Q(x0,y0)')
line16 = ax1.text(0.6,5, 'M(a0,b0)')
line18 = plt.xlim(-5,5)
line17 = plt.ylim(0,25)
plt.savefig('C:/Users/BRIAR/Desktop/index.png')
plt.show()

The mininum distance: 1.943317035
The mininum distance: 1.9433170350024023
class ‘list'

講解Python3中NumPy數(shù)組尋找特定元素下標(biāo)的兩種方法

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI