How do you extract diagonal elements of submatrix matrix?

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!

 Accepted 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);

10 Comments

This works well! Thanks Ameer!
Ameer,
I am also interested in the diagonal cells (or blocks) of the B matrix. I tried using the following code as such:
diag_B = cellfun(@diag, B);
But it didn't work.
After reading about 'cellfun', this might not be the right funciton for me to extract the information I need because the output of 'cellfun' is an array. And the diagonal of B would be submatrices. I hope you understand my point.
Please advise. Thanks so much in advance!
Can you further explain? Is B a cell array? Have you tried with 'uni', 0 option as shown in my answer?
Hi Ameer,
Sorry for the late response.
No, B is not a cell array. It is the same B you simulated, which is in it contains blocks of 3x3 matrices.
I wanted to get the diagonal blocks of that B. I've tried with uni, 0 option, didn't work either.
I also tried using the command:
diag_B = blkdiag(B{:}) but that didn't work either.
Please advise. Thanks a lot Ameer!
But this line already extract the diagonal elements
diag_all = cellfun(@diag, B, 'uni', 0);
Can you post the error message?
Hi Ameer,
Yes, you are right that functon:
diag_all = cellfun(@diag, B, 'uni', 0);
gives you the diagonal elements of the diagonal blocks of B. So, that's done. I am so grateful for your solution.
My follow up question is how can i get JUST the diagonal blocks of B (in your simulation, thats the 3x3 matrices) and separate them as a separate and individual matrices. The logical function would be to use blkdiag(B) but that didn't seem to work.
Again, thanks Ameer!
Try this
diag_all = cellfun(@diag, B, 'uni', 0);
diag_B = blkdiag(diag_all{:})
Hey Ameer,
Thanks for the response. I've done the codes you suggested above but to no avail. It keeps giving me the following errors:
Error using zeros
Out of memory. Type HELP MEMORY for your options.
Error in blkdiag (line 43)
y = zeros(p1(end),m1(end),outclass); %Preallocate
I tried just simplifying it with:
diag_B = blkdiag(B{:}) AND diag_B = diag(B);
But it still won't work.
Is there any other solution. I can't imagine doing this manually when working with a 2464x2464 matrix.
Thanks Ameer. Anything helps.
Hey guys,
What does the 'uni' argument pertain to?
Couldnt find anything on the web other than the name-value pair 'UniformOutput'. Is this the same?
Thanks!

Sign in to comment.

More Answers (0)

Categories

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!