How to write a symmetric matrix in Matlab given the dimension?
9 次查看(过去 30 天)
显示 更早的评论

Hello everyone, I'm fairly new to Matlab & I was wondering if you could help me out with something. I need to write in Matlab code the Cholesky analysis & test it on a specific matrix. I've already written the code but I've been testing it on random symmetric/positive-definite matrixes & it works just fine. But my assignment is to test it on the one I have attached as an image. The dimension n should be 64. I know it might seem stupid & I'm pretty much done with the code of my assignment, I just don't know how to input this specific matrix considering it has "..."? How do you generate this when you have the dimension and "part" of the matrix? I'm looking forward to your answers. Thanks a lot!
0 个评论
回答(2 个)
Matt J
2015-1-18
编辑:Matt J
2015-1-18
You could use spdiags. In what form do you have the non-zero matrix values?
It also looks like your matrix is mostly Toeplitz apart from the initial and final rows. You could generate the bulk of the matrix by doing
An = toeplitz([6 -4 1 zeros(1,63)]);
and then modify a few of the entries manually.
John D'Errico
2015-1-18
Well, the simple answer is to use spdiags. If you want the matrix to be full when you use it, then just add a call to full afterwards.
Or use diag for each diagonal, then sum the resulting matrices. Each call to diag will let you create a diagonal matrix with one of those diagonals. So this will require only 5 calls to diag.
Or, you could use subsindex.
Or, you could use sparse. Again, if your goal is a full matrix, then make it so as the last step.
To be honest, a simple set of loops would work too, though not nearly do pretty, or as fast.
None of the above solutions are difficult, although the last three of them will take a bit more work. I'd suggest either spdiags or diag. With diag for example, see if first you can create a diagonal matrix with your main diagonal. Then try making a matrix with the proper sub and super diagonals, etc. Then see how you might combine them all into your goal.
Why not try one of those methods, and then if you have a further problem with it (like something went wrong) then add a comment here showing what you tried, and I'll help you more. The point is, you will learn more by making a stab at it than by me doing it directly for you.
3 个评论
John D'Errico
2015-1-18
编辑:John D'Errico
2015-1-18
Why not try diag first? For example, what does this do:
diag([9,repmat(6,1,61),5,1])
Next, see that you can form a sub or super diagonal matrix by supplying a third argument to diag. Next, see what happens when you add two of those matrices together, formed to have different diagonals.
diag([repmat(-4,1,62),-2],1)
See that this will let you build up the matrix you desire.
If the matrix is symmetric, then the upper and lower triangles are identical. This is by definition of a symmetric matrix. I think you are talking about the elements at the bottom end of those diagonals.
You keep on talking about the upper and lower triangles of a matrix. For example, given the 5x5 matrix A,
A = magic(5)
A =
17 24 1 8 15
23 5 7 14 16
4 6 13 20 22
10 12 19 21 3
11 18 25 2 9
The LOWER triangle is given by tril.
tril(A)
ans =
17 0 0 0 0
23 5 0 0 0
4 6 13 0 0
10 12 19 21 0
11 18 25 2 9
And the upper triangle is given by triu.
triu(A)
ans =
17 24 1 8 15
0 5 7 14 16
0 0 13 20 22
0 0 0 21 3
0 0 0 0 9
See that both triangles share the main diagonal, although we can control what parts we extract using tril and triu.
So, as you can see, IF the matrix was symmetric, then the upper and lower triangles must look alike.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!