How to write a symmetric matrix in Matlab given the dimension?

8 visualizzazioni (ultimi 30 giorni)
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!

Risposte (2)

Matt J
Matt J il 18 Gen 2015
Modificato: Matt J il 18 Gen 2015
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.
  5 Commenti
Maura Tinney
Maura Tinney il 18 Gen 2015
Modificato: Maura Tinney il 18 Gen 2015
Thank you again! May I ask what does -1:1 do in the spdiags function? Also, since I have 6,6,6...6,6,6 or -4,-4,-4...-4,-4,-4 that I need to depict diagonically, how am I supposed to do that in the spdiags function? Since I have 64 elements that it'd be inconvenient to write them manually?
Matt J
Matt J il 18 Gen 2015
Modificato: Matt J il 18 Gen 2015
A = spdiags(B,d,m,n) creates an m-by-n sparse matrix by taking the columns of B and placing them along the diagonals specified by d.
You can fill vectors and matrices with repetitions of a number similar to the following,
>> z(1:8)=6
z =
6 6 6 6 6 6 6 6 6 6

Accedi per commentare.


John D'Errico
John D'Errico il 18 Gen 2015
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 Commenti
John D'Errico
John D'Errico il 18 Gen 2015
Modificato: John D'Errico il 18 Gen 2015
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.
Maura Tinney
Maura Tinney il 18 Gen 2015
Yes, according to the definition of a "symmetric matrix" Aij=Aji & this condition must not be overruled. Thank you again for your help, I think I know how to go from there.

Accedi per commentare.

Categorie

Scopri di più su Creating and Concatenating Matrices in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by