0

我有一个列向量,我正在尝试编写一个以某种方式实现可变窗口函数的函数。这意味着我想选择一行并跳过多行(这是可变部分),但不仅要跳过,我还必须将跳过行中的一列的值设置为等于它们之前的所选行同一列的。该列是:

----------
  P1
----------
  P2
----------
  P3
----------
  P4
----------

所以目标是用 P1 P1 P3 P3 P4 P4 创建一个新列...变量部分的意思是通过改变函数中的一个变量,可以用 P1 P1 P1 P4 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 的值。

任何帮助表示赞赏。

4

2 回答 2

0

答案如下,包括窗口之间的比较并将所有结果保存在数组中:

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 
于 2014-02-15T22:11:41.583 回答
0

这是我的建议。我觉得简单一点。为了使索引向量具有正确的形状,我借用了解决方案形式A similar function to R's rep in Matlab。这与您的问题非常相似。

%# 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.

于 2014-02-15T22:19:55.310 回答