0

我有一个使用四个索引 i、j、k 和 l 创建的矩阵。我使用以下方案来创建矩阵,

for i = 1:N
 for j = 1:2
  for k = 1:2
   for l = 1:N
    Matrix(l,2(j+2*i-3)+k) = Value
   end
  end
 end
end

当我这样做时,它会产生一个矩阵,其变化如下:

                              Columns
         i = 1  i = 1  i = 1  i = 1  i = 2  i = 2  i = 2  i = 2 ...   
         j = 1  j = 1  j = 2  j = 2  j = 1  j = 1  j = 2  j = 2 ...
         k = 1  k = 2  k = 1  k = 2  k = 1  k = 2  k = 1  k = 2 ...
l = 1
l = 2
  .
  .
  . 

不幸的是,我将其编入了错误的索引,需要对其进行更改。我不想改变任何计算。我需要一个将上述矩阵更改为以下内容的代码:

                               Columns
             l = 1  l = 1  l = 2  l = 2  l = 1  l = 1  l = 2  l = 2 ...   
             k = 1  k = 2  k = 1  k = 2  k = 1  k = 2  k = 1  k = 2 ...
i = 1  j = 1               
i = 1  j = 2              
i = 2  j = 1   
i = 2  j = 2    
      .
      .  
      .

换句话说,一个看起来像的代码,

for i = 1:N
 for j = 1:2
  for k = 1:2
   for l = 1:N
    Matrix(New Index) = Matrix(Old Index)
   end
  end
 end
end

我该怎么做呢?谢谢。

4

1 回答 1

0

让我们看看我是否正确理解了这个问题:

你有一个Matrix(称之为a)大小N x 4N,你想创建一个矩阵(称之为b)大小2N x 2N

的元素a由集合定义,(i,j,k,l)使得:

a = f(1,1,1,1) f(1,1,2,1) f(1,2,1,1) f(1,2,2,1) ... f(N,2,2,1)
    f(1,1,1,2) f(1,1,2,2) f(1,2,1,2) f(1,2,2,2) ... f(N,2,2,2)
    f(1,1,1,3) f(1,1,2,3) f(1,2,1,3) f(1,2,2,3) ... f(N,2,2,3)
    f(1,1,1,4) f(1,1,2,4) f(1,2,1,4) f(1,2,2,4) ... f(N,2,2,4)
    ...
    f(1,1,1,N) f(1,1,2,N) f(1,2,1,N) f(1,2,2,N) ... f(N,2,2,N)

现在你想要b这样:

b = f(1,1,1,1) f(1,1,2,1) f(1,1,1,2) f(1,1,2,2) ... f(1,1,2,N)
    f(1,2,1,1) f(1,2,2,1) f(1,2,1,2) f(1,2,2,2) ... f(1,2,2,N)
    f(2,1,1,1) f(2,1,2,1) f(2,1,1,2) f(2,1,2,2) ... f(2,1,2,N)
    f(2,2,1,1) f(2,2,2,1) f(2,2,1,2) f(2,2,2,2) ... f(2,2,2,N)
    ...
    f(N,2,1,N) f(N,2,2,N) f(N,2,1,N) f(N,2,2,N) ... f(N,2,2,N)

上面是正确的,那么您需要做的就是:

b = zeros(2*N);
b(:, 1:2:end) = a(:, 1:2:end)';
b(:, 2:2:end) = a(:, 2:2:end)';

例子:

n = 5;
a = 1:(4*n*n);
a = reshape(a, n, 4*n);

b = zeros(2*n);
b(:, 1:2:end) = a(:, 1:2:end)';
b(:, 2:2:end) = a(:, 2:2:end)';

输出:

a =

 Columns 1 through 13:

     1     6    11    16    21    26    31    36    41    46    51    56    61
     2     7    12    17    22    27    32    37    42    47    52    57    62
     3     8    13    18    23    28    33    38    43    48    53    58    63
     4     9    14    19    24    29    34    39    44    49    54    59    64
     5    10    15    20    25    30    35    40    45    50    55    60    65

 Columns 14 through 20:

    66    71    76    81    86    91    96
    67    72    77    82    87    92    97
    68    73    78    83    88    93    98
    69    74    79    84    89    94    99
    70    75    80    85    90    95   100

b =

     1     6     2     7     3     8     4     9     5    10
    11    16    12    17    13    18    14    19    15    20
    21    26    22    27    23    28    24    29    25    30
    31    36    32    37    33    38    34    39    35    40
    41    46    42    47    43    48    44    49    45    50
    51    56    52    57    53    58    54    59    55    60
    61    66    62    67    63    68    64    69    65    70
    71    76    72    77    73    78    74    79    75    80
    81    86    82    87    83    88    84    89    85    90
    91    96    92    97    93    98    94    99    95   100
于 2013-08-05T20:40:26.310 回答