0
votes

I have a cell array where each cell contains different size of square adjacency matrix(in MATLAB) for example

A = 29x29 double
      30x30 double 
      24x24 double 
      10x10 double

Now I want to create One Block diagonal matrix B from each cell of A such that each cell Of A is in the diagonal in B. Example

 B = [29X29] 0 0 0 0 0 0 0 0  0 0
        0   0 [30x30] 0 0 0 0 0 0 0 
        0   0  0   0  [24X24] 0 0 0
        0   0  0   0   0   0 [10x10]

so B would NxN where N = 29+30+24+10

I tried with the following code but it did not work.

 function B =blockD(A)
n=size(A,1);
for i = 1:n
    B=blkdiag(A{i});
end
end

Also at the end I have to row normalised matrix B

1
Looks like matlab.MaxPowers
yeah MATLAB @user202729Vishnu Kant
So you want each element of each adjacency matrix to be on the main diag of B?MaxPowers

1 Answers

0
votes

Just use B = blkdiag(A{:})

octave:4> A(1) = [1,2; 3, 4]
A = {3x1x3 Cell Array}
octave:5> A(2) = [1,2; 6, 4]
A = {3x1x3 Cell Array}
octave:6> A(3)  = [1, 7; 5 8]
A = {3x1x3 Cell Array}
octave:7> B = blkdiag(A{:})
B =

   1   2   0   0   0   0
   3   4   0   0   0   0
   0   0   1   2   0   0
   0   0   6   4   0   0
   0   0   0   0   1   7
   0   0   0   0   5   8