讲解Python3中NumPy数组寻找特定元素下标的两种方法

时间:2021-05-22

引子

Matlab中有一个函数叫做find,可以很方便地寻找数组内特定元素的下标,即:Find indices and values of nonzero elements。
这个函数非常有用。比如,我们想计算图1中点Q(x0, y0)抛物线的最短距离。一个可以实施的方法是:计算出抛物线上所有点到Q点的距离,找到最小值,用find函数找到最小值对应的下标,即M点横坐标和纵坐标对应的元素的下标,M点到Q点的距离就是最短距离。


首先给出Matlab使用find函数实现的代码:

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);

NumPy中的where函数

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

具体实现代码如下:

import numpy as npimport mathimport matplotlib.pyplot as plta = np.linspace(-5, 5, 10000)b = a * ax0 = 4y0 =4num = 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'

List中的index函数

Syntax: List.index(aimElement)
注意:此处需将NumPy数组转换成List格式的数据。
具体实现代码如下:

import numpy as npimport mathimport matplotlib.pyplot as plta = np.linspace(-5, 5, 10000)b = a * ax0 = 4y0 =4num = 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'

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章