MATLAB: How do you extract diagonal elements of submatrix matrix

cell arraysdiagonalMATLAB and Simulink Student Suitematrixmatrix arraymatrix manipulationsubmatrices

Hi, need help:
I have a (nxn) square matrix A. I then created submatrices of (kxk) within matrix A. How do I extract only the diagonal (kxk) submatrices into a separate vector? The command diag(A) does not work; this is maybe because of the submatrices elements that I created. Please advise Thanks!

Best Answer

Are the submatrices in cell array? For example, try something like this
n = 9;
A = rand(n);
k = 3;
B = mat2cell(A, k*ones(1,n/k), k*ones(1,n/k));
diag_all = cellfun(@diag, B, 'uni', 0);