Reputation:
On the diagonal I have n+1, nxn blocks of zeros, but then in the other n blocks on that row, I have a certain matrix.
See picture here:
What I want to do with these P blocks is have a list of the matrices I would like to try in each one of these Pn blocks and test for certain matrix properties.
These are my troubles: 1. Finding a way to make a list of matrices I would like to iterate through. (I am new to MATLAB and this doesn't come as easy as other languages like python) 2. Making a nested loop that tries each permutation of my matrix with each of the P blocks different possible matrices.
To clarify each P has the same possible matrices. This list of possible matrices is of size n!.
This seems like a rather simple task that I am struggling incredibly with. So far I only have:
%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
Yes, very naive. Obviously I would like Pa
and Pb
in one list that I would be able to iterate through on the matrix.
If someone could point me in the right direction, I would very much appreciate it.
Upvotes: 4
Views: 208
Reputation: 112779
1- Answer to your question before the edit (there are n x n blocks, each block has n x n entries):
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
In this example I'm using a list of 3 matrices, and n
is 3. So there are 729 results. Here are the first few:
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- Modifications in order to answer your question after the edit (there are (n+1) x (n+1) blocks, each block has n x n entries):
As per your edit, the number of blocks is now larger. In this case I'm using an example with 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
First few results:
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
Upvotes: 1