Create diagonals along a block matrix

조회 수: 2 (최근 30일)
sc
sc 2021년 12월 15일
댓글: sc 2021년 12월 15일
Good Morning,
I have a 4x4 matrix containing values only in the first column and all 0's in the remaining spaces. The values contained in each cell are actually vectors of multiple values.
I'd like to make the first value (vector) of the first row be carried over to the second row of the second column, the third row of the third column, and so on. I would like to do the same for the second value (vector) of the first row, which should be reported in the third row of the second column, and so on, in order to create diagonals along the matrix containing the same values.
How can I do this?
Thank you so much!!
  댓글 수: 4
Jan
Jan 2021년 12월 15일
편집: Jan 2021년 12월 15일
Sorry, this is a Matlab forum. This is not a Matlab matrix:
A =
[0,0]
[1,1]
[2,1]
[3,3]
Please use Matlab syntax, because this is the common convention here. Thanks.
A matrix has 2 diagonals only. I assume you mean the sub- and super-diagonals?
"B=blkdiag(A{:})" - Now A is a cell array? Sorry for beeing nitpicking, but the more the readers have to guess, the more unlikely is a useful answer.
sc
sc 2021년 12월 15일
편집: sc 2021년 12월 15일
Unfortunately, I haven't used Matlab for very long, so I'm struggling to make myself understood. Anyway, I mean that A is a cell matrix, where each cell contains a vector. I would like to get another cell matrix (called B), which as shown in the code above, presents the cells diagonally. Is it possible to do this?

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

채택된 답변

Jan
Jan 2021년 12월 15일
편집: Jan 2021년 12월 15일
With some bold guessing and knowing, that this might be more confusing than useful:
The input A is a cell vector and the wanted output B a cell matrix:
A = {[0,0]; ...
[1,1]; ...
[2,1]; ...
[3,3]};
H = flipud(hankel(4:-1:1)) % The display is cropped:
H = 4×4
1 0 0 0 2 1 0 0 3 2 1 0 4 3 2 1
B = cell(numel(A), numel(A));
B(H ~= 0) = A(H(H~= 0))
B = 4×4 cell array
{[0 0]} {0×0 double} {0×0 double} {0×0 double} {[1 1]} {[ 0 0]} {0×0 double} {0×0 double} {[2 1]} {[ 1 1]} {[ 0 0]} {0×0 double} {[3 3]} {[ 2 1]} {[ 1 1]} {[ 0 0]}
% Or maybe:
H2 = max(1, flipud(hankel(4:-1:1))) % The display is cropped:
H2 = 4×4
1 1 1 1 2 1 1 1 3 2 1 1 4 3 2 1
B2 = A(H2)
B2 = 4×4 cell array
{[0 0]} {[0 0]} {[0 0]} {[0 0]} {[1 1]} {[0 0]} {[0 0]} {[0 0]} {[2 1]} {[1 1]} {[0 0]} {[0 0]} {[3 3]} {[2 1]} {[1 1]} {[0 0]}
  댓글 수: 1
sc
sc 2021년 12월 15일
Thank you, the second approach is the correct one!

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Operating on Diagonal Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by