MATLAB Answers

0

How to extract all the diagonal blocks of size 2x2 from NxN matrix?

Asked by riya sharma on 21 Mar 2019
Latest activity Answered by Walter Roberson
on 21 Mar 2019
Suppose we have a square matrix A of size 40 x40 as follow
How to extract all the diagonal blocks of size 2 x 2 from the matrix A ?

  0 Comments

Sign in to comment.

1 Answer

Answer by Walter Roberson
on 21 Mar 2019

temp = repmat({ones(2,2)}, 1, 20);
L = blkdiag(temp{:});
output = reshape(A(L), 2, 2, []);
This will produce a 2 x 2 x 20 result where each pane is one of the 2 x 2 block diagonals.

  0 Comments

Sign in to comment.