【问题标题】:Permuting Through Certain Blocks of a Matrix通过矩阵的某些块置换
【发布时间】:2014-03-13 22:38:22
【问题描述】:

在对角线上我有 n+1,nxn 个零块,但是在该行的其他 n 个块中,我有一个特定的矩阵。

看图:

我想要对这些 P 块做的是列出我想在每个 Pn 块中尝试的矩阵并测试某些矩阵属性。

这些是我的烦恼: 1. 找到一种方法来制作我想迭代的矩阵列表。 (我是 MATLAB 新手,这不像 python 等其他语言那么容易) 2.创建一个嵌套循环,尝试我的矩阵的每个排列,每个 P 块不同的可能矩阵。

为了澄清每个 P 具有相同的可能矩阵。 此可能矩阵的列表大小为 n!。

这似乎是一个相当简单的任务,我正在努力解决。 到目前为止,我只有:

 %For n = 2
 Pa = [1 0; 0 1];
 Pb = [0 1; 1 0];
 Z = [0 0; 0 0];

 row1 = [Z Pa Pa];
 row2 = [Pa Z Pa];
 row3 = [Pa Pa Z];

 C = [row1; row2; row3];

 trc = trace(C*C*C);
 if trc == 0
 disp(C);
 end

 %Now need to try Pb for one of the Pa

是的,很天真。显然,我希望将PaPb 放在一个列表中,以便我能够在矩阵上进行迭代。

如果有人能指出我正确的方向,我将不胜感激。

【问题讨论】:

  • 澄清一下:如果你有 6 个非零块(如图所示)和 3 个矩阵的列表,你想得到所有 3^6 个可能的复合矩阵,对吧?
  • 如果列表包含 n!矩阵,得到的大矩阵的总数是 n!^(n*(n-1))。我说的对吗?
  • 你说得对。对困惑感到抱歉。我应该添加 m = n!。所以会有(矩阵列表的大小)^(非零块的数量)。是的。
  • 现在我又糊涂了。您更新的图像没有 n、nxn 个零块。它有 n+1,nxn 个零块。
  • 在你的例子中 C = kron(~eye(3), Pa)

标签: algorithm matlab matrix permutation


【解决方案1】:

1- 在编辑之前回答您的问题(有 n x n 个块,每个块有 n x n 个条目):

n = 3; 
example = [1 2 3; 4 5 6; 7 8 9];
matrices = cat(3, example, 10*example, 100*example);
%// This is the list. Each third-dim slice is a matrix

[aux{1:n}] = deal(ones(n));
nz = ~blkdiag(aux{:}); %// template for filling result matrix
m = size(matrices,3); %// number of matrices in list
T = n^2; %// size of result matrix
N = n*(n-1); %// number of blocks
for ii = 0:m^N-1 %// number of results  
    ind = dec2base(ii,m,N)-'0'+1; %// indices of matrices to be used
    result = zeros(T); %// initiallize to zeros
    result(nz) = permute(reshape(matrices(:,:,ind),[n n n-1 n]),[1 3 2 4]);
    %// fill in matrices given by ind
    disp(result)
end

在这个例子中,我使用了一个包含 3 个矩阵的列表,n 是 3。所以有 729 个结果。以下是前几个:

 0     0     0     1     2     3     1     2     3
 0     0     0     4     5     6     4     5     6
 0     0     0     7     8     9     7     8     9
 1     2     3     0     0     0     1     2     3
 4     5     6     0     0     0     4     5     6
 7     8     9     0     0     0     7     8     9
 1     2     3     1     2     3     0     0     0
 4     5     6     4     5     6     0     0     0
 7     8     9     7     8     9     0     0     0

 0     0     0     1     2     3     1     2     3
 0     0     0     4     5     6     4     5     6
 0     0     0     7     8     9     7     8     9
 1     2     3     0     0     0    10    20    30
 4     5     6     0     0     0    40    50    60
 7     8     9     0     0     0    70    80    90
 1     2     3     1     2     3     0     0     0
 4     5     6     4     5     6     0     0     0
 7     8     9     7     8     9     0     0     0

 0     0     0     1     2     3     1     2     3
 0     0     0     4     5     6     4     5     6
 0     0     0     7     8     9     7     8     9
 1     2     3     0     0     0   100   200   300
 4     5     6     0     0     0   400   500   600
 7     8     9     0     0     0   700   800   900
 1     2     3     1     2     3     0     0     0
 4     5     6     4     5     6     0     0     0
 7     8     9     7     8     9     0     0     0

 0     0     0     1     2     3    10    20    30
 0     0     0     4     5     6    40    50    60
 0     0     0     7     8     9    70    80    90
 1     2     3     0     0     0     1     2     3
 4     5     6     0     0     0     4     5     6
 7     8     9     0     0     0     7     8     9
 1     2     3     1     2     3     0     0     0
 4     5     6     4     5     6     0     0     0
 7     8     9     7     8     9     0     0     0

 0     0     0     1     2     3    10    20    30
 0     0     0     4     5     6    40    50    60
 0     0     0     7     8     9    70    80    90
 1     2     3     0     0     0    10    20    30
 4     5     6     0     0     0    40    50    60
 7     8     9     0     0     0    70    80    90
 1     2     3     1     2     3     0     0     0
 4     5     6     4     5     6     0     0     0
 7     8     9     7     8     9     0     0     0


2- 修改以回答您的问题编辑后(有 (n+1) x (n+1) 个块,每个块有 n x n 个条目):强>

根据您的编辑,块的数量现在更大。在这种情况下,我使用n=2 的示例。

n = 2; 
matrices = cat(3, [1 2; 3 4], [10 20; 30 40], [100 200; 300 400]);
%// This is the list. Each third-dim slice is a matrix

[aux{1:n+1}] = deal(ones(n));
nz = ~blkdiag(aux{:}); %// template for filling result matrix
m = size(matrices,3); %// number of matrices in list
T = n*(n+1); %// size of result matrix
N = (n+1)*n; %// number of blocks
R = m^N; %// number of results  
for ii = 0:R-1
    ind = dec2base(ii,m,N)-'0'+1; %// indices of matrices to be used
    result = zeros(T); %// initiallize to zeros
    result(nz) = permute(reshape(matrices(:,:,ind),[n n n n+1]),[1 3 2 4]);
    %// fill in matrices given by ind
    disp(result)
end

前几个结果:

 0     0     1     2     1     2
 0     0     3     4     3     4
 1     2     0     0     1     2
 3     4     0     0     3     4
 1     2     1     2     0     0
 3     4     3     4     0     0

 0     0     1     2     1     2
 0     0     3     4     3     4
 1     2     0     0    10    20
 3     4     0     0    30    40
 1     2     1     2     0     0
 3     4     3     4     0     0

 0     0     1     2     1     2
 0     0     3     4     3     4
 1     2     0     0   100   200
 3     4     0     0   300   400
 1     2     1     2     0     0
 3     4     3     4     0     0

 0     0     1     2    10    20
 0     0     3     4    30    40
 1     2     0     0     1     2
 3     4     0     0     3     4
 1     2     1     2     0     0
 3     4     3     4     0     0

 0     0     1     2    10    20
 0     0     3     4    30    40
 1     2     0     0    10    20
 3     4     0     0    30    40
 1     2     1     2     0     0
 3     4     3     4     0     0

【讨论】:

  • 哇,真漂亮。我很确定这正是我需要使用的。我认为这正是我描述得很差的需要它做的事情(对不起!)我会对其进行测试并报告。
  • 这个答案适用于您的第一张图片,而不是第二张(当块较少时)。有问题吗?
  • 完美运行。非常感谢你。我需要去学习你使用的所有这些功能。
  • 我认为不应该。如果我自己无法弄清楚如何将其应用于更大或更小的矩阵,那我根本就不应该从事编程工作。
  • 你最后的评论很棒,我完全同意。尽管如此,我一直在弄清楚应该更改哪些内容以匹配您的第二张图片(其中块数较大)。我将其作为答案的第二部分包含在内
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-12-16
  • 2013-09-25
  • 1970-01-01
  • 1970-01-01
  • 2013-03-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多