小编xan*_*eon的帖子

在Python中有效地查找大于阈值的第一个样本(和MATLAB比较)

threshold我希望只找到a signal大于a 的第一个样本,而不是找到列表或数组中大于特定的所有样本/数据点threshold.信号可能会多次超过阈值.例如,如果我有一个示例信号:

signal = [1, 2, 3, 4, 4, 3, 2, 1, 0, 3, 2, 1, 0, 0, 1, 1, 4, 8, 7, 6, 5, 0]
Run Code Online (Sandbox Code Playgroud)

和a threshold = 2,然后

signal = numpy.array(signal)
is_bigger_than_threshold = signal > threshold
Run Code Online (Sandbox Code Playgroud)

会给我所有signal超过的值threshold.但是,每当信号变得大于阈值时,我想只得到第一个样本.因此,我将浏览整个列表并进行布尔比较

first_bigger_than_threshold = list()
first_bigger_than_threshold.append(False)
for i in xrange(1, len(is_bigger_than_threshold)):
    if(is_bigger_than_threshold[i] == False):
        val = False
    elif(is_bigger_than_threshold[i]):
        if(is_bigger_than_threshold[i - 1] == False):
            val = True
        elif(is_bigger_than_threshold[i - 1] == True):
            val = …
Run Code Online (Sandbox Code Playgroud)

python matlab numpy threshold

5
推荐指数
1
解决办法
5430
查看次数

使用Python中的matplotlib绘制许多数据点

我最近从MATLAB切换到Python进行数据分析,我使用matplotlib来显示数据.如果我想要显示的数据点数量很少,这可以正常工作.但是,如果我想想象,例如

import matplotlib.pyplot as plt
signal = [round(random.random() * 100) for i in xrange(0, 1000000)]
plt.plot(signal)
plt.show()
Run Code Online (Sandbox Code Playgroud)

我收到一个错误:

Exception in Tkinter callback
Traceback (most recent call last):
  File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 1489, in __call__
    return self.func(*args)
  File "/usr/lib/pymodules/python2.7/matplotlib/backends/backend_tkagg.py", line 276, in resize
    self.show()
  File "/usr/lib/pymodules/python2.7/matplotlib/backends/backend_tkagg.py", line 348, in draw
    FigureCanvasAgg.draw(self)
  File "/usr/lib/pymodules/python2.7/matplotlib/backends/backend_agg.py", line 451, in draw
    self.figure.draw(self.renderer)
  File "/usr/lib/pymodules/python2.7/matplotlib/artist.py", line 55, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
  File "/usr/lib/pymodules/python2.7/matplotlib/figure.py", line 1034, in draw
    func(*args)
  File "/usr/lib/pymodules/python2.7/matplotlib/artist.py", line 55, in draw_wrapper
    draw(artist, …
Run Code Online (Sandbox Code Playgroud)

matlab matplotlib python-2.7

5
推荐指数
1
解决办法
3121
查看次数

在Python列表中有效查找索引(与MATLAB相比)

我很难找到在Python列表中查找索引的有效解决方案.到目前为止,我测试过的所有解决方案都比MATLAB中的"find"功能慢.我刚刚开始使用Python(因此,我不是很有经验).

在MATLAB中我会使用以下内容:

a = linspace(0, 1000, 1000); % monotonically increasing vector
b = 1000 * rand(1, 100); % 100 points I want to find in a
for i = 1 : numel(b)
    indices(i) = find(b(i) <= a, 1); % find the first index where b(i) <= a
end
Run Code Online (Sandbox Code Playgroud)

如果我使用MATLAB的arrayfun(),我可以加快这个过程.在Python中我尝试了几种可能性.我用了

for i in xrange(0, len(b)):
   tmp = numpy.where(b[i] <= a)
   indices.append(tmp[0][0])
Run Code Online (Sandbox Code Playgroud)

这花费了很多时间,特别是如果a非常大的话.如果b排序比我可以使用

for i in xrange(0, len(b)):
    if(b[curr_idx] <= a[i]):
        indices.append(i)
        curr_idx += 1
    if(curr_idx >= len(b)):
        return indices
        break
Run Code Online (Sandbox Code Playgroud)

这比numpy.where()解决方案快得多,因为我只需要在列表中搜索一次,但这仍然比MATLAB解决方案慢. …

python matlab numpy list

2
推荐指数
1
解决办法
252
查看次数

标签 统计

matlab ×3

numpy ×2

python ×2

list ×1

matplotlib ×1

python-2.7 ×1

threshold ×1