Creating a block matrix

I have two matrices A and D with dimensions mxm. I want to create a size B mn x mn with these two block matrices. For example, if n = 5, then the output will be

B= D A A A A
   A D 0 0 0
   A 0 D 0 0
   A 0 0 D 0
   A 0 0 0 D

I managed to create this form with many for loops, but I would like to get a faster solution with the functions provided by matlab.

+6
source share
4 answers

This should do the trick:

m = 3;
n = 5;
mn = m*n;

A_val = 4;
D_val = 2;

% Just an example, you could use rand(m) instead...
A = repmat(A_val,m);
D = repmat(D_val,m);

D_cell = repmat({D},1,n);
B = blkdiag(D_cell{:});

idx_1 = 1:m;
idx_2 = (m+1):mn;
B(idx_2,idx_1) = repmat(A,n-1,1);
B(idx_1,idx_2) = repmat(A,1,n-1);

Output:

B =
     2     2     2     4     4     4     4     4     4     4     4     4     4     4     4
     2     2     2     4     4     4     4     4     4     4     4     4     4     4     4
     2     2     2     4     4     4     4     4     4     4     4     4     4     4     4
     4     4     4     2     2     2     0     0     0     0     0     0     0     0     0
     4     4     4     2     2     2     0     0     0     0     0     0     0     0     0
     4     4     4     2     2     2     0     0     0     0     0     0     0     0     0
     4     4     4     0     0     0     2     2     2     0     0     0     0     0     0
     4     4     4     0     0     0     2     2     2     0     0     0     0     0     0
     4     4     4     0     0     0     2     2     2     0     0     0     0     0     0
     4     4     4     0     0     0     0     0     0     2     2     2     0     0     0
     4     4     4     0     0     0     0     0     0     2     2     2     0     0     0
     4     4     4     0     0     0     0     0     0     2     2     2     0     0     0
     4     4     4     0     0     0     0     0     0     0     0     0     2     2     2
     4     4     4     0     0     0     0     0     0     0     0     0     2     2     2
     4     4     4     0     0     0     0     0     0     0     0     0     2     2     2

The average value tic-toccompared to 1000iterations 0.00018 seconds.

See the following links for more information on the features used:

+3
source

This is easy to do with the kronecker product kron:

m = 3; % size of the blocks
n = 5; % number of blocks
A = rand(m); % insert you matrices here
D = rand(m);
maskA = zeros(n); % maskA is the block structure of A
maskA(1,:) = 1;
maskA(:,1) = 1;
maskA(1,1) = 0;
maskD = eye(n); %maskD is the block structure of D

B = kron(maskA,A) + kron(maskD,D);
+3
source

:

A = [10 20; 30 40]; % square matrix
D = [50 60; 70 80]; % square matrix
n = 5; % positive integer
tmp_A = repmat({A}, 1, n-1);
tmp_D = repmat({D}, 1, n-1);
result = [D, horzcat(tmp_A{:}); vertcat(tmp_A{:}), blkdiag(tmp_D{:})]
+2

cell2mat:

C = {zeros(size(A)), D , A};
mat = ones(n);
mat(1:n+1:end)=2;
mat(1,2:end)= 3;
mat(2:end,1)= 3;
out = cell2mat(C(mat));
+2

All Articles