How do you extract diagonal elements of submatrix matrix?

조회 수: 7 (최근 30일)
BooDee
BooDee 2020년 12월 2일
댓글: Vishnu Pradeep 2021년 12월 2일
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!

채택된 답변

Ameer Hamza
Ameer Hamza 2020년 12월 2일
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
BooDee
BooDee 2020년 12월 11일
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.
Vishnu Pradeep
Vishnu Pradeep 2021년 12월 2일
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!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by