I need to transform this matrix, taking the three diagonal lines that have numbers and make them their own matrices. My matrix, however, is 124x124. I do not want to write a 1x123 matrix out that has 122 of the same number and 1 zero. Is there a way use ones() for this?

댓글 수: 2

Adam Danz
Adam Danz 2021년 2월 15일
What 3 diagonal lines?
The description of the data and the description of the goal are not clear.
A demo using a smaller matrix would likely help.
Anastasia Zistatsis
Anastasia Zistatsis 2021년 2월 15일
편집: Adam Danz 2021년 2월 15일
[2 13 0 0 0 0;
1 2 13 0 0 0;
0 1 2 13 0 0;
0 0 1 2 13 0;
0 0 0 1 2 13];
These are the three diagonals I was talking about: [ 13 13 13 13 13 0], zeros(1,5)*2, and [0 1 1 1 1]
For a much larger matrix, is there a function in matlab that can make it easier to type out the first and third vectors shown above?

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

답변 (1개)

Jan
Jan 2021년 2월 15일

1 개 추천

A = [2 13 0 0 0 0;
1 2 13 0 0 0;
0 1 2 13 0 0;
0 0 1 2 13 0;
0 0 0 1 2 13];
d1 = diag(A, 0) % [2 2 2 2 2],'
d2 = diag(A, 1) % [13 13 13 13 13].'
d3 = diag(A, -1) % [1 1 1 1].'
I do not understand, how you want to get "[13 13 13 13 13 0]" (especially the trailing 0), "zeros(1,5)*2" (zeros times anything is still zeros?), and [0 1 1 1 1] (again the leading zero)?

댓글 수: 1

Anastasia Zistatsis
Anastasia Zistatsis 2021년 2월 16일
It's part of the answer matrix that I should have put in my question! Diag() helps though, thank you!

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

카테고리

도움말 센터File Exchange에서 Operating on Diagonal Matrices에 대해 자세히 알아보기

태그

질문:

2021년 2월 15일

댓글:

2021년 2월 16일

Community Treasure Hunt

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

Start Hunting!

Translated by