使用一维作为循环中剩余维度的迭代器迭代 3D numpy [英] Iterating over 3D numpy using one dimension as iterator remaining dimensions in the loop
问题描述
尽管有许多与迭代 3D 数组相关的类似问题,并且在尝试了 numpy 的 nditer
等一些函数后,我仍然对如何实现以下目标感到困惑:>
我有一个维度为 (30, 11, 300) 的信号,它是包含 300 个信号点的 11 个信号的 30 次试验.
让这个信号用变量x_
我有另一个函数,它将 (11, 300) 矩阵作为输入并将其绘制在 1 个图上(11 个信号包含 300 个信号点绘制在单个图上).将此函数设为sliding_window_plot
.
目前,我可以让它做到这一点:
x_plot = x_[0,:,:]对于 i 在范围内(x_.shape[0]):滑动窗口图(x_plot [:,:])
绘制相同(第一次试验)11 个信号,在 1 个图上包含 300 个点,共 30 次.我希望它绘制第 i 组信号.不是每次都对信号进行第一次(第 0 次)试验.有关如何尝试此操作的任何提示?
您应该能够使用 for
循环遍历第一个维度:
for s in x_:slide_window_plot(s)
每次迭代 s
将是下一个形状数组 (11, 300).
Despite there being a number of similar questions related to iterating over a 3D array and after trying out some functions like nditer
of numpy, I am still confused on how the following can be achieved:
I have a signal of dimensions (30, 11, 300) which is 30 trials of 11 signals containing 300 signal points.
Let this signal be denoted by the variable x_
I have another function which takes as input a (11, 300) matrix and plots it on 1 graph (11 signals containing 300 signal points plotted on a single graph). Let this function be sliding_window_plot
.
Currently, I can get it to do this:
x_plot = x_[0,:,:]
for i in range(x_.shape[0]):
sliding_window_plot(x_plot[:,:])
which plots THE SAME (first trial) 11 signals containing 300 points on 1 plot, 30 times. I want it to plot the i'th set of signals. Not the first (0th) trial of signals everytime. Any hints on how to attempt this?
You should be able to iterate over the first dimension with a for
loop:
for s in x_:
sliding_window_plot(s)
with each iteration s
will be the next array of shape (11, 300).
这篇关于使用一维作为循环中剩余维度的迭代器迭代 3D numpy的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!