【问题标题】:Matlab: How to turn a vector of 2928x1 into a vector of 8784x1 with specific intervals of elements?Matlab:如何将 2928x1 的向量转换为具有特定元素间隔的 8784x1 向量?
【发布时间】:2017-03-20 11:37:07
【问题描述】:

我正在尝试将具有 2928 个值的向量转换为具有 8784 个值的向量。第一个向量是一个具有 3 小时间隔的信息的向量,我想要一个每小时向量,其中每 3 小时添加一次这些值,其余的应该用 NaN 填充。 我的第一种方法是创建一个包含 8784 个值的 NaN 向量,但后来我无法创建一个适用于此的“for 循环”。

为了简单起见,我试着用一个例子来解释(n是最小向量的值的个数):

S_3h = ones(n,1); % this acts as the small vector that has only information each 3hours
B_h = nan(3*n,1); %this is the created hourly vector that I want to fulfill

想要的结果是:

B_h = [1 nan nan 1 nan nan 1 nan nan 1 nan nan ...]

你能帮帮我吗? 非常感谢您!

【问题讨论】:

    标签: matlab loops for-loop vector


    【解决方案1】:

    仅使用步长不同于 1 的索引。在这种情况下,步长为 3。

    B_h(1:3:end) = S_3h
    

    【讨论】:

      【解决方案2】:

      已经有两个很好的答案,所以仅针对运动(以及kron...),这里有一个线性解决方案:

      S_3h = randi(10,1,n);
      B_h=kron(S_3h,[1 NaN NaN]);
      

      【讨论】:

        【解决方案3】:

        Zizy Archer's solution 很好(也许你应该使用),但下面是另一种选择。

        S_3h = ones(n,1);
        B_h = nan(3,n); % notice the different indices
        
        B_h(1,:) = S_3h; % the top row contains the non-NaN values. This is common to all methods.
        B_h = B_h(:); % reshape to a column vector
        

        做的有点不同:

        B_h = reshape( S_3h.' .* [1; NaN(1,2)],[],1);              % R2016b onward
        B_h = reshape( bsxfun(@times, S_3h.',[1; NaN(2,1)]),[],1 ); % R2007a onward
        

        如果你有图像处理工具箱,你也可以像这样使用padarray函数:

        B_h = reshape(padarray(S_3h, [0 2], NaN, 'post').', [], 1);
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2016-02-01
          • 1970-01-01
          • 2014-12-25
          • 2011-10-07
          • 1970-01-01
          • 2020-11-19
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多