【问题标题】:Constructing matrices in MATLAB在 MATLAB 中构造矩阵
【发布时间】:2015-04-28 20:28:41
【问题描述】:

我正在尝试在 MATLAB 中构造几个矩阵。编程不是我的强项,我无法深入探讨。我知道我应该嵌套 for 循环,但不知道怎么做。我有一个数据数组 y 和 T 值。我需要设置一个滞后 m,然后找到 Tm=T-2m+1。矩阵 Ymin 和 Yplus 的阶数为 m x Tm,但不知道如何设置它们。括号中是数据在数组中的位置。对不起格式。矩阵是:

      y(m)      y(m+1)   ...   y(T-m)
      y(m-1)    y(m)     ...   y(T-m-1) 
        .
        .
        .
       y(1)      y(2)    ...   y(Tm)

      y(m+1)    y(m+2)     ...   y(T-m+1)
      y(m+2)    y(m+3)     ...   y(T-m-2) 
        .
        .
        .
       y(2m)      y(2m+1)  ...   y(T)

【问题讨论】:

    标签: matlab


    【解决方案1】:

    使用bsxfun 获取这两个矩阵的一种方法 -

    T = numel(y) %// number of elements in y
    
    idx1 = bsxfun(@plus,[m:-1:1]',0:Tm-1)
    out1 = y(idx1)
    
    idx2 = bsxfun(@plus,[m+1:2*m]',0:Tm-1)
    out2 = y(idx2)
    

    【讨论】:

    • 试着把T = numel(y)变成Tm=numel(y)
    • @andrew 这个问题说yT 值。此外,在问题Tm 中还有其他内容。
    【解决方案2】:

    希望下面的matlab代码可以帮到你!

    %by Mark 4/28/2015
    
    clear all;
    %%%%%%%%%%%%%%%%% take an example
    
    m=4; 
    T=10;
    y=zeros(1,T);
    
    for i=1:(T)
        y(i)=i;
    end
    
    %%%%%%%%%%%%%%%%%%%%%% calculate Ymin
    
    count_i=0;
    
    for i=m:1:(T-m)
    
        count_i=count_i+1;
    
        count_j=0;
    
        for j=i:(-1):(i-m+1)
            count_j=count_j+1;
            Ymin(count_j,count_i)=y(j);
        end
    end
    
    Ymin
    
    %%%%%%%%%%%%%%%%%%%%%% calculate Ymax
    
    count_i=0;
    
    for i=(m+1):1:(T-m+1)
        count_i=count_i+1;
        count_j=0;
        for j=i:1:(i+m-1)
            count_j=count_j+1;
            Ymax(count_j,count_i)=y(j);
        end
    end
    
    Ymax
    

    【讨论】:

    • 嗨,马克,欢迎来到 Stackoverflow!如果您在答案中将代码缩进 4 个空格,它将被正确格式化为代码,因此更易于阅读。一个简单的方法是选择所有代码并按ctrl-k。这将一次性解决所有问题。
    • 非常感谢您的帮助!
    【解决方案3】:

    您可以使用hankeltoeplitz 函数。

    第一次使用矩阵

    flipud(hankel(y(1:m), y(m:Tm))
    

    toeplitz(y(m:-1:1), y(m:Tm))
    

    对于第二个矩阵使用

    hankel(y(m+1:2*m), y(2*m:T))
    

    【讨论】:

      猜你喜欢
      • 2011-03-17
      • 1970-01-01
      • 2014-10-24
      • 1970-01-01
      • 2014-04-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多