Greetings
I want to write this matrix (50*50) into the matlab and I have no idea how can I do it?

댓글 수: 1

John D'Errico
John D'Errico 2021년 5월 19일
DGM is correct. As shown, the matrix "SHOULD" have one more column than it has rows.

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

 채택된 답변

David Hill
David Hill 2021년 5월 19일

0 개 추천

a=diag(ones(1,50))+diag(2*ones(1,49),1);

추가 답변 (1개)

DGM
DGM 2021년 5월 19일
편집: DGM 2021년 5월 19일

1 개 추천

Maybe I'm missing something, but that description looks ambiguous. At one end, you have 1 on the 0th diagonal and 2 on the next diagonal. Then at the other end, 2 is on the 0th diagonal and 1 is on the -1th diagonal. Since the matrix is square, the runs must change somewhere in between (in the middle?).
If the runs stayed on their respective diagonals, you could just do
n = 10;
A = diag(ones(n,1),0) + diag(2*ones(n-1,1),1)
A = 10×10
1 2 0 0 0 0 0 0 0 0 0 1 2 0 0 0 0 0 0 0 0 0 1 2 0 0 0 0 0 0 0 0 0 1 2 0 0 0 0 0 0 0 0 0 1 2 0 0 0 0 0 0 0 0 0 1 2 0 0 0 0 0 0 0 0 0 1 2 0 0 0 0 0 0 0 0 0 1 2 0 0 0 0 0 0 0 0 0 1 2 0 0 0 0 0 0 0 0 0 1
or something similar.
If the runs shift in the middle of the array:
n = 5;
A = diag(ones(n,1),0) + diag(2*ones(n-1,1),1);
B = diag(2*ones(n,1),0) + diag(ones(n-1,1),-1);
C = blkdiag(A,B)
C = 10×10
1 2 0 0 0 0 0 0 0 0 0 1 2 0 0 0 0 0 0 0 0 0 1 2 0 0 0 0 0 0 0 0 0 1 2 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 0 0 1 2 0 0 0 0 0 0 0 0 0 1 2 0 0 0 0 0 0 0 0 0 1 2 0 0 0 0 0 0 0 0 0 1 2
You could also do that by specifying the diagonal and off-diagonal vectors explicitly.

댓글 수: 8

Kakashi Hatake
Kakashi Hatake 2021년 5월 20일
Thank you DGM, that was a good attention. Actualy, my matrix is a little more complex, and I could not write it in Matlab. The matrix I try to write is the following matrix from the raw 3th the order of number is reapeted. Also, g, f, v are known variables.
DGM
DGM 2021년 5월 20일
Well, so long as you can construct one of the diagonal blocks, you should be able to flip it and assemble the two using blkdiag()
Kakashi Hatake
Kakashi Hatake 2021년 5월 20일
Thank you very much, I will try using your suggestion.
Something like:
f = 12;
g = 20;
v = 30;
N = 10; % must be even
n = N/2;
A = diag(ones(n-2,1),-2) ...
+ diag([v; -4*ones(n-2,1)],-1) ...
+ diag([g; 5; 6*ones(n-2,1)],0) ...
+ diag([-2*f; -4*ones(n-2,1)],1) ...
+ diag([2; ones(n-3,1)],2);
B = blkdiag(A,rot90(A,2))
B = 10×10
20 -24 2 0 0 0 0 0 0 0 30 5 -4 1 0 0 0 0 0 0 1 -4 6 -4 1 0 0 0 0 0 0 1 -4 6 -4 0 0 0 0 0 0 0 1 -4 6 0 0 0 0 0 0 0 0 0 0 6 -4 1 0 0 0 0 0 0 0 -4 6 -4 1 0 0 0 0 0 0 1 -4 6 -4 1 0 0 0 0 0 0 1 -4 5 30 0 0 0 0 0 0 0 2 -24 20
Kakashi Hatake
Kakashi Hatake 2021년 5월 20일
편집: Kakashi Hatake 2021년 5월 20일
Many thanks for your help and time. but, the matrix you insert is not my matrix, at 5th row after 6 there is not -4 and 1. Also in 6th and 7th rows the order of number is not 1, -4, 6, -4, 1.
I would be grateful if you could kindly help me with that.
I'm still working on the assumption that the matrix is square. That would mean that the diagonal vector would be
[g 5 6 6 6 ... 6 6 6 5 g]
The way the diagram is illustrated only shows the diagonal blocks as 4x6, possibly because that's sufficient to describe them since the rest of the interior values are simply repeated. If the matrix is square, then the diagram is simply confusing because the diagonal vector doesn't meet itself.
If the matrix is not square, then there isn't enough information to know which diagonals belong where. I'm assuming you don't want a literal 8x12 matrix.
However, there is a good possibility that this is what was intended:
f = 12;
g = 20;
v = 30;
n = 10; % must be even
A = diag(ones(n-2,1),-2) ...
+ diag([v; -4*ones(n-3,1); v],-1) ...
+ diag([g; 5; 6*ones(n-4,1); 5; g],0) ...
+ diag([-2*f; -4*ones(n-3,1); -2*f],1) ...
+ diag([2; ones(n-4,1); 2],2)
A = 10×10
20 -24 2 0 0 0 0 0 0 0 30 5 -4 1 0 0 0 0 0 0 1 -4 6 -4 1 0 0 0 0 0 0 1 -4 6 -4 1 0 0 0 0 0 0 1 -4 6 -4 1 0 0 0 0 0 0 1 -4 6 -4 1 0 0 0 0 0 0 1 -4 6 -4 1 0 0 0 0 0 0 1 -4 6 -4 2 0 0 0 0 0 0 1 -4 5 -24 0 0 0 0 0 0 0 1 30 20
That's still assuming that it's a square array. The only difference is that it uses the full diagonals instead of blocks
Kakashi Hatake
Kakashi Hatake 2021년 5월 20일
that is completely what I want. Many thank DGM. that was really nice of you.
Actually, I may have made a mistake with the symmetry there.
f = 12;
g = 20;
v = 30;
n = 10; % must be even
A = diag([ones(n-3,1); 2],-2) ...
+ diag([v; -4*ones(n-3,1); -2*f],-1) ...
+ diag([g; 5; 6*ones(n-4,1); 5; g],0) ...
+ diag([-2*f; -4*ones(n-3,1); v],1) ...
+ diag([2; ones(n-3,1)],2)
A = 10×10
20 -24 2 0 0 0 0 0 0 0 30 5 -4 1 0 0 0 0 0 0 1 -4 6 -4 1 0 0 0 0 0 0 1 -4 6 -4 1 0 0 0 0 0 0 1 -4 6 -4 1 0 0 0 0 0 0 1 -4 6 -4 1 0 0 0 0 0 0 1 -4 6 -4 1 0 0 0 0 0 0 1 -4 6 -4 1 0 0 0 0 0 0 1 -4 5 30 0 0 0 0 0 0 0 2 -24 20

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

카테고리

도움말 센터File Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기

태그

질문:

2021년 5월 19일

댓글:

DGM
2021년 5월 20일

Community Treasure Hunt

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

Start Hunting!

Translated by