Matlab-用一个较小的矩阵创建一个较大的矩阵,其中包含空格

Matlab-用一个较小的矩阵创建一个较大的矩阵,其中包含空格,matlab,matrix,Matlab,Matrix,假设我有一个3x3矩阵a,我想把它变成5x5矩阵B,但矩阵a有以下内容: 1 2 3 4 5 6 7 8 9 1 0 2 0 3 0 0 0 0 0 4 0 5 0 6 0 0 0 0 0 7 0 8 0 9 由此产生的较大矩阵B需要具有以下内容: 1 2 3 4 5 6 7 8 9 1 0 2 0 3 0 0 0 0 0 4 0 5 0 6 0 0 0 0 0 7 0 8 0 9 我知道这可以通过以下顺序使用一些FOR来完成: %% We get the dimensions of

假设我有一个3x3矩阵a,我想把它变成5x5矩阵B,但矩阵a有以下内容:

1 2 3
4 5 6
7 8 9
1 0 2 0 3
0 0 0 0 0
4 0 5 0 6
0 0 0 0 0
7 0 8 0 9
由此产生的较大矩阵B需要具有以下内容:

1 2 3
4 5 6
7 8 9
1 0 2 0 3
0 0 0 0 0
4 0 5 0 6
0 0 0 0 0
7 0 8 0 9
我知道这可以通过以下顺序使用一些FOR来完成:

  %% We get the dimensions of our matrix. 
  [xLength, yLength] = size(InMat); 

  %% We create a matrix of the double size.
  NewInMat = zeros(xLength * 2, yLength * 2);

  %% We prepare the counters to fill the new matrix.
  XLenN = (xLength * 2) -1;
  YLenN = (yLength * 2) - 1;

  for i = 1 : XLenN
      for j = 1 : YLenN
         if mod(i, 2) ~= 0
           if mod(j, 2) ~= 0
              NewInMat(i, j) = InMat(i, j);
           else
              NewInMat(i,j) = mean([InMat(i, j - 1), InMat(i, j + 2)]);  
           end
         end
    end
end
但我想知道是否有一个更简单的方法,或者Matlab是否有一个工具来完成这项任务。非常感谢

您可以使用索引:

InMat = [...
1 2 3
4 5 6
7 8 9];
s = size(InMat)*2-1;
NewInMat(1:2:s(1), 1:2:s(2)) = InMat;
此处同时分配和填写了NewInMat。

您可以使用索引:

InMat = [...
1 2 3
4 5 6
7 8 9];
s = size(InMat)*2-1;
NewInMat(1:2:s(1), 1:2:s(2)) = InMat;
在这里,NewInMat是同时分配和填充的