[英]I need to find the location of a value in one numpy array and use it to refer to a value in the same location of another numpy array
I am writing code to analyze accelerometer data on a raspberry pi. 我正在编写代码以分析树莓派上的加速度计数据。 The sensor data is output to a single txt file with columns separated by \\t.
传感器数据输出到单个txt文件,其列用\\ t分隔。 I imported the text file using numpy.loadtxt and unpacked it into separate arrays.
我使用numpy.loadtxt导入了文本文件,并将其解压缩到单独的数组中。 I can perform things like trapz and cumtrapz on the arrays.
我可以在阵列上执行trapz和cumtrapz之类的操作。
This data will be used in combination with another sensor that will output a specific time of an event. 该数据将与另一个传感器一起使用,该传感器将输出事件的特定时间。 I want to take that time, find the closest logged time from my sensor and correspond it to values from the other arrays.
我想花那个时间,从我的传感器中找到最接近的记录时间,并将其与其他数组的值相对应。
I tried using numpy.where with a specific time value that i knew was in the list and got an output of "(array([], dtype=int32),)" 我尝试使用numpy.where和一个我知道在列表中的特定时间值,并得到“(array([],dtype = int32),)”的输出
Here is the code I ran. 这是我运行的代码。 I'm sure I misused at least one thing.
我确定我至少滥用了一件事。 I am still very much a beginner in Python and coding in general...
我仍然还是Python和编码一般的初学者...
import logging
import sys
import numpy as np
from scipy import integrate
x,y,z,t=np.loadtxt('a.txt', dtype={'names':['x','y','z','t'],
'formats':['f4','f4','f4','f4']},unpack='true')
p = integrate.trapz(integrate.cumtrapz(x, t, initial=0), t)
ti = np.where(x==1.5670002)
print ti
print p
The full output from that is 完整的输出是
(array([], dtype=int32),)
0.0114166
So I was searching x for a value from t. 所以我在x中寻找t的值。 it is now outputting (array([101]),)
现在正在输出(array([101]),)
How would I print that corresponding number from another array? 我如何从另一个数组中打印相应的数字?
Here is my solution: 这是我的解决方案:
import logging
import sys
import numpy as np
from scipy import integrate
x,y,z,t=np.loadtxt('a.txt', dtype={'names':['x','y','z','t'],
'formats':['f4','f4','f4','f4']},unpack='true')
p = integrate.cumtrapz(integrate.cumtrapz(x, t, initial=0), t)
t0=input("What is reference time?")
ti = np.where(t>=t0)[0][0]
if t[ti]-t0 <= t0-t[ti-1]:
t1 = ti
else:
t1 = ti-1
print ('closest time was {0:0.4f}\ndisplacement at that time was {1:0.4f}' .format(t[t1],p[t1]))
output is 输出是
closest time was 1.1991
displacement at that time was 0.0100
Seems to be working. 似乎正在工作。 I will have to add error messages for when the reference time is outside of a usable range.
当参考时间超出可用范围时,我将不得不添加错误消息。 Would love some constructive criticism though.
虽然会喜欢一些建设性的批评。 Any commands that you think would work better/faster than what I have used?
您认为有比我使用的命令更好/更快的命令吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.