【问题标题】:Choosing Specific Rows of a Matrix Matlab (Windowing Function)选择矩阵 Matlab 的特定行(窗口函数)
【发布时间】:2014-02-13 16:50:15
【问题描述】:

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

----------
  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 的值。

感谢任何帮助。

【问题讨论】:

  • 请展示你的努力。

标签: matlab function matrix rows windowing


【解决方案1】:

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

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 

【讨论】:

    【解决方案2】:

    这是我的建议。我觉得简单一点。 为了使索引向量具有正确的形状,我借用了解决方案形式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.

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-20
      • 2015-03-30
      • 1970-01-01
      • 2018-06-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多