如何在 m 的窗口中选择序列的 n 个元素? (MATLAB)
快速 MATLAB 问题。 在“m”的窗口中选择一定数量的元素“n”的最佳/最有效的方法是什么。换句话说,我想选择序列的前 50 个元素,然后是元素 10-60,然后是元素 20-70 等。 现在,我的序列是矢量格式(但这可以很容易地改变)。
编辑: 我正在处理的序列太长,无法存储在 RAM 中。我需要能够创建窗口,然后调用我想要分析/执行另一个命令的窗口。
Quick MATLAB question.
What would be the best/most efficient way to select a certain number of elements, 'n' in windows of 'm'. In other words, I want to select the first 50 elements of a sequence, then elements 10-60, then elements 20-70 ect.
Right now, my sequence is in vector format(but this can easily be changed).
EDIT:
The sequences that I am dealing with are too long to be stored in my RAM. I need to be able to create the windows, and then call upon the window that I want to analyze/preform another command on.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您是否有足够的 RAM 来在内存中存储 50×nWindow 数组?在这种情况下,您可以一次性生成窗口,然后将处理应用到每一列
Do you have enough RAM to store a 50-by-nWindow array in memory? In that case, you can generate your windows in one go, and then apply your processing on each column
为了补充 Kerrek 的答案:如果你想循环执行它,你可以使用类似的东西
To complement Kerrek's answer: if you want to do it in a loop, you can use something like
你的问题描述有点问题。您说您想要“选择序列的前 50 个元素,然后选择元素 10-60...”;然而,这将转化为选择元素:
第一个序列应该是 0-10 以适应模式,这当然在 MATLAB 中没有意义,因为数组使用单索引。为了解决这个问题,下面的算法使用一个名为 startIndex 的变量来指示从哪个元素开始序列采样。
您可以通过构造索引数组以矢量化方式完成此操作。创建一个由每个序列的起始索引组成的向量。为了重用,我将序列的长度、序列开始之间的步长以及最后一个序列的开始作为变量。在您描述的示例中,序列的长度应为 50,步长应为 10,最后一个序列的开始取决于输入数据的大小和您的需求。
创建一些示例数据:
创建序列起始索引的向量:
创建索引数组以索引到数据数组:
最后,使用此索引数组来引用数据数组:
There's a slight issue with the description of your problem. You say that you want "to select the first 50 elements of a sequence, then elements 10-60..."; however, this would translate to selecting elements:
That first sequence should be 0-10 to fit the pattern which of course in MATLAB would not make sense since arrays use one-indexing. To address this, the algorithm below uses a variable called startIndex to indicate which element to start the sequence sampling from.
You could accomplish this in a vectorized way by constructing an index array. Create a vector consisting of the starting indices of each sequence. For reuse sake, I put the length of the sequence, the step size between sequence starts, and the start of the last sequence as variables. In the example you describe, the length of the sequence should be 50, the step size should be 10 and the start of the last sequence depends on the size of the input data and your needs.
Create some sample data:
Create a vector of the start indices of the sequences:
Create an array of indices to index into the data array:
Finally, use this index array to reference the data array:
使用
(start : step : end)
索引:v(1:1:50)
、v(10:1:60)
等如果step
是1
,则可以省略它:v(1:50)
。Use
(start : step : end)
indexing:v(1:1:50)
,v(10:1:60)
, etc. If thestep
is1
, you can omit it:v(1:50)
.考虑以下矢量化代码:
结果(为简洁而截断):
事实上,该代码改编自信号处理工具箱中现已弃用的 SPECGRAM 函数(只需
编辑 specgram.m
即可查看代码)。我省略了对序列进行零填充的部分,以防滑动窗口未均匀划分整个序列(例如
x=1:105
),但如果您需要该功能,您可以轻松地再次添加它们...Consider the following vectorized code:
The result (truncated for brevity):
In fact, the code was adapted from the now deprecated SPECGRAM function from the Signal Processing Toolbox (just do
edit specgram.m
to see the code).I omitted parts that zero-pad the sequence in case the sliding windows do not evenly divide the entire sequence (for example
x=1:105
), but you can easily add them again if you need that functionality...