【问题标题】:Matlab: matrix made of vector forward decreasingMatlab:由向量向前递减的矩阵
【发布时间】:2017-07-27 12:59:20
【问题描述】:

我正在改变矩阵,使用矢量化并避免循环。

首先我有:

V = [x1 x2 x3 x4 x5]

我设法用hankel 函数创建:

M = [1 2 3 4 5; 
     2 3 4 5 0;
     3 4 5 0 0;
     4 5 0 0 0;
     5 0 0 0 0]

repmat:

A = [x1 x2 x3 x4 x5;
     x1 x2 x3 x4 x5; 
     x1 x2 x3 x4 x5;  
     x1 x2 x3 x4 x5; 
     x1 x2 x3 x4 x5]

我想知道如何结合MA得到

[x1 x2 x3 x4 x5; 
 x2 x3 x4 x5  0; 
 x3 x4 x5  0  0; 
 x4 x5  0  0  0; 
 x5  0  0  0  0]

我认为这并不难,但一直无法弄清楚! 非常感谢任何帮助。谢谢

编辑:

Hankel (V) 成功了,感谢 Wolfie。 我没有具体说明我在执行速度上有所提高(这是在大多数情况下避免循环所固有的)。

【问题讨论】:

    标签: matlab matrix


    【解决方案1】:

    您可以使用索引很容易地做到这一点。

    V 保留为行向量,但在开头添加 0 而不是使用 repmat

    V = [x1 x2 x3 x4 x5];
    A = [0 V];           % add 0 to beginning of vector to account for 0s in output
    M = hankel(1:5) + 1; % Get indices of A which correspond to location in output
    output = A(M);
    
    >> output = [x1 x2 x3 x4 x5
                 x2 x3 x4 x5  0
                 x3 x4 x5  0  0
                 x4 x5  0  0  0
                 x5  0  0  0  0]
    

    或者一开始就不生成M,直接使用hankel

    V = [x1 x2 x3 x4 x5];
    output = hankel(V)  
    
    >> output = [x1 x2 x3 x4 x5
                 x2 x3 x4 x5  0
                 x3 x4 x5  0  0
                 x4 x5  0  0  0
                 x5  0  0  0  0]
    

    【讨论】:

    • 谢谢 Wolfie,是的,我意识到从一开始就直接使用 hankel 就可以了。尽管与循环相比,在执行速度方面没有任何提升(但我在最初的问题中没有具体说明这是我所追求的......)。
    猜你喜欢
    • 1970-01-01
    • 2014-01-02
    • 1970-01-01
    • 2021-12-04
    • 1970-01-01
    • 1970-01-01
    • 2014-07-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多