inserting matrix into matlab
이전 댓글 표시
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
2021년 5월 19일
DGM is correct. As shown, the matrix "SHOULD" have one more column than it has rows.
채택된 답변
추가 답변 (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)
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)
You could also do that by specifying the diagonal and off-diagonal vectors explicitly.
댓글 수: 8
Kakashi Hatake
2021년 5월 20일
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
2021년 5월 20일
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))
Kakashi Hatake
2021년 5월 20일
편집: Kakashi Hatake
2021년 5월 20일
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)
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
2021년 5월 20일
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)
카테고리
도움말 센터 및 File Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
