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

1 Comment
John D'Errico
on 19 May 2021
DGM is correct. As shown, the matrix "SHOULD" have one more column than it has rows.
Accepted Answer
More Answers (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 Comments
Kakashi Hatake
on 20 May 2021
DGM
on 20 May 2021
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
on 20 May 2021
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
on 20 May 2021
Edited: Kakashi Hatake
on 20 May 2021
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
on 20 May 2021
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)
Categories
Find more on Resizing and Reshaping Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!