>我有一个列向量,我正在尝试编写一个以某种方式实现变量窗口函数的函数。这意味着我想选择一行并跳过许多行(这是变量部分),但不仅跳过,我还必须将跳过行中其中一列的值设置为等于同一列之前所选行的值。该列为:
----------
P1
----------
P2
----------
P3
----------
P4
----------
所以目标是创建一个带有 P1 P1 P3 P3 P4 P4 的新列...... 变量部分意味着通过更改函数中的变量,可以使用 P1 P1 P1 P1 P4 P4 P7 P7 P7 ...
我厌倦了这样的东西:(实现第一种情况)
% column vector containing P values a
a ;
delay = 0;
% f parameter to enter the delay processing
f = 2;
r = length(a);
i = 1;
while(i <= r)
if(mod(i, f) == 0)
for j = 0 : delay
a(i + j) = a(i - 1);
end
i = i + delay + 1;
else
i = i + 1;
end
end
我认为问题在于使用 MOD 函数或选择 f 的值。
任何帮助不胜感激。
如下,包括窗口之间的比较和将所有结果保存在数组中:
a = v;
r = length(a);
i = 1;
all_Results = [];
Vectors = [];
for window =1:128 ;
while(i < r)
for w = 1 : window;
if (i < r )
i = i+1;
a(i ) = a(i-1);
end
end
i = i + 1 ;
end
equal_elements = length(find(a==t));
accuracy = equal_elements/ length(t);
Results = [ window , accuracy ];
Vectors = [Vectors , a ];
all_Results = [all_Results; Results];
a = v;
i = 1;
end
这是我的建议。我认为简单一点。为了使索引向量具有正确的形状,我借用了与 Matlab 中 R 的 rep 类似的函数形式的解决方案。这与您的问题非常相似。
%# random vector, parameter i to enter the delay processing
vector = rand(1000000,1);
i=2;
%# entries of vector to be duplicated
repeat = 1:i:length(vector);
%# fill gaps in this index vector and cut to same length
matrix = repmat(repeat,i,1);
index = matrix(:);
index(length(vector)+1:end) = [];
%# generate desired result
vector = vector(index);
我的机器上这些参数的计时:Elapsed time is 0.055114 seconds.