nadineDinDin
nadineDinDin

Reputation: 75

Block matrix creation

I have two matrices A and D with sizes mxm. I want to create with these two a block matrix B size mn x mn . 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 have managed to create this form with many for loops but I would like a quicker solution with functions provided by matlab.

Upvotes: 6

Views: 167

Answers (4)

Tommaso Belluzzo
Tommaso Belluzzo

Reputation: 23675

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

Average tic-toc performance over 1000 iterations 0.00018 seconds.

For more details about the employed functions, refer to the following links:

Upvotes: 3

Luis Mendo
Luis Mendo

Reputation: 112689

Here's a way:

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{:})]

Upvotes: 2

rahnema1
rahnema1

Reputation: 15837

Here is a way using 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));

Upvotes: 2

flawr
flawr

Reputation: 11628

It is easy to do with 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);

Upvotes: 3

Related Questions