快速MATLAB问题。在“m”窗口中选择“n”个元素的最佳/最有效方法是什么?换句话说,我想选择序列的前50个元素,然后选择第10-60个元素,然后选择第20-70个元素等。目前,我的序列采用向量格式(但这很容易改变)。
编辑:我正在处理的序列太长,无法存储在我的RAM中。我需要能够创建窗口,然后调用我想要分析/执行其他命令的窗口。
编辑:我正在处理的序列太长,无法存储在我的RAM中。我需要能够创建窗口,然后调用我想要分析/执行其他命令的窗口。
n = 50
m = 10;
for i=1:m:length(v)
w = v(i:i+n);
% Do something with w
end
您的计算机内存是否足够存储一个大小为50×n的窗口数组?如果是,您可以一次性生成所有窗口,然后对每列应用处理操作。
%# idxMatrix has 1:50 in first col, 11:60 in second col etc
idxMatrix = bsxfun(@plus,(1:50)',0:10:length(yourVector)-50); %'#
%# reshapedData is a 50-by-numberOfWindows array
reshapedData = yourVector(idxMatrix);
%# now you can do processing on each column, e.g.
maximumOfEachWindow = max(reshapedData,[],1);
windowNumber
是13,则窗口#13为stepSize *(windowNumber-1)+(1:windowLength)
。 stepSize
是起始索引之间的差异(在您的情况下为10),windowLength
是窗口中元素的数量(在您的情况下为50)。 - Jonas>> sampleData(index)这段代码是一个示例数据的索引,它返回一个5行3列的矩阵。
回答 =
98 83 84 65 9 81 81 14 7 46 18 40 44 40 53
请考虑以下向量化代码:
x = 1:100; %# an example sequence of numbers
nwind = 50; %# window size
noverlap = 40; %# number of overlapping elements
nx = length(x); %# length of sequence
ncol = fix((nx-noverlap)/(nwind-noverlap)); %# number of sliding windows
colindex = 1 + (0:(ncol-1))*(nwind-noverlap); %# starting index of each
%# indices to put sequence into columns with the proper offset
idx = bsxfun(@plus, (1:nwind)', colindex)-1; %'
%# apply the indices on the sequence
slidingWindows = x(idx)
slidingWindows =
1 11 21 31 41 51
2 12 22 32 42 52
3 13 23 33 43 53
...
48 58 68 78 88 98
49 59 69 79 89 99
50 60 70 80 90 100
edit specgram.m
即可查看代码)。x=1:105
),但如果您需要该功能,可以轻松地再次添加它们...(start : step : end)
索引: v(1:1:50)
,v(10:1:60)
等等。如果 step
是 1
,可以省略它: v(1:50)
。
1-50,11-60,21-70,...
对吧? - Amro