algorithm - 置换矩阵的某些 block

标签 algorithm matlab matrix permutation

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

请看这里的图片:

enter image description here

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

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

为了阐明每个 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 在一个列表中,这样我就可以在矩阵上进行迭代。

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

最佳答案

1- 在编辑之前回答您的问题(有 n x n block ,每个 block 有 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) 个 block ,每个 block 有 n x n 个条目):

根据您的编辑, block 的数量现在更大了。在这种情况下,我使用了 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

关于algorithm - 置换矩阵的某些 block ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22392486/

相关文章:

algorithm - 如何检查是否可以在字典中找到所有子字符串

c++ - Matlab 如何知道有一个 .mex64 文件并避免无限循环 "compiling"

matlab - 在 Matlab 中更改直方图的轴

python - 以特定方式合并单位矩阵

algorithm - 使用伴随矩阵求根

c - 到达终点所需的最小跳跃次数的线性时间算法

c++ - 洗牌?

python - 如何并行化一个长字符串的正则表达式搜索?

image - 我应该使用哪种方法来查找图像中像素的梯度方向?

python - 矩阵程序,使用python中的绑定(bind)来停止/启动