Transfer through specific matrix blocks

On the diagonal, I have n + 1, nxn blocks of zeros, but then in the other n blocks in this row I have a specific matrix.

See image here:

enter image description here

What I want to do with these P-blocks is a list of matrices that I would like to try in each of these Pn blocks and check certain properties of the matrix.

These are my problems: 1. Finding a way to make a list of matrices that I would like to skip. (I am new to MATLAB, and it is not as simple as other languages ​​such as python) 2. Creating a nested loop that tries every permutation of my matrix with each of P blocks of different possible matrices.

To clarify that each P has the same possible matrices. This list of possible matrices has size 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

, . , , Pa Pb, .

- , .

+4
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
+1

Source: https://habr.com/ru/post/1531663/


All Articles