Matlab Code Help: Read Sparse Matrix, Calculate Eigenvalues

In summary, the person is seeking help in creating a MATLAB code that can read a sparse matrix and calculate its eigenvalues, as well as show its sparsity pattern. They have the data in compressed sparse row (CSR), compressed sparse column (CSC), and co-ordinate formats, but prefer using CSR or CSC due to storage constraints. They usually use Fortran but need the mathematical functions available in MATLAB. They also mention the online documentation and suggest using the eig and sparse functions, as well as dlmread or fscanf for reading the data. They have not worked with sparse matrices in MATLAB before and suggest starting with the sparse command and looking for information on the Mathworks website.
  • #1
confi999
19
0
Hello.
Can anyone help me in producing a Matlab code that will read a sparse matrix and then calculate the eigenvalues of it, show the sparsity pattern of it etc. I have the the sparse matrix data in compressed sparse row (CSR), compressed sparse column (CSC) formats and also in Co-ordinate format. But as co-ordinate format takes large storage I would prefer CSR or CSC format. I usually use Fortran but Matlab has some readily available mathematical functions and therefore I need this MATLAB code. Any support on Matlab code is highly appreciated. Thank you
 
Physics news on Phys.org
  • #2
Welcome to PhysicsForums!

The online MATLAB documentation is really good (the in-program one is decent, but, of course, it's within the MATLAB text environment). For instance, you can bring up help on the eig eigenvalue function using:
>> help eig

Or, go to the more complete documentation at the Mathworks website (note the note regarding sparse matrices):
http://www.mathworks.com/access/helpdesk/help/techdoc/ref/eig.html

If they're stored in text format, my first inclination is that you'd probably need the dlmread (or more likely, C-style fscanf) function just to get the numbers into MATLAB. You'll probably still need to write a script to handle your CSR and CSC formats.
http://www.mathworks.com/access/helpdesk/help/techdoc/ref/dlmread.html
http://www.mathworks.com/access/helpdesk/help/techdoc/ref/fscanf.html

I've never worked with sparse matrices (not as anything different from regular matrices, at any rate), and obviously, not within the MATLAB environment. Just from reading, the sparse command looks to be able to inflate / compress sparse matrices:
http://www.mathworks.com/access/helpdesk/help/techdoc/ref/sparse.html

However, if there's anything to be found, it might be found by starting here:
http://www.mathworks.com/access/helpdesk/help/techdoc/math/f6-9182.html [Broken]
 
Last edited by a moderator:
  • #3


Sure, I can provide some guidance on how to approach this problem in Matlab. First, you will need to use the "spconvert" function to convert your sparse matrix data from CSR or CSC format to a Matlab sparse matrix. This will allow you to efficiently store and manipulate the data in Matlab.

Next, you can use the "eigs" function to calculate the eigenvalues of the sparse matrix. This function uses an iterative algorithm to find the largest eigenvalues and corresponding eigenvectors, which can be helpful for large sparse matrices.

To show the sparsity pattern of the matrix, you can use the "spy" function. This will create a visual representation of the non-zero elements in the matrix.

I also recommend looking into the "sparsity" function, which will give you the percentage of non-zero elements in the matrix. This can be useful for determining the level of sparsity and if it is appropriate for using sparse matrix methods.

I hope this helps get you started on your Matlab code. If you need further assistance, there are many online resources and forums dedicated to Matlab programming that can provide additional support. Good luck!
 

1. What is Matlab?

Matlab is a high-level programming language and interactive environment for numerical computation, visualization, and programming. It is commonly used in scientific and engineering applications.

2. What is a sparse matrix?

A sparse matrix is a special type of matrix that contains mostly zero values. It is used to efficiently store and manipulate large matrices with a lot of zero values, which can save memory and computation time.

3. How do I read a sparse matrix in Matlab?

To read a sparse matrix in Matlab, you can use the spconvert function or the spload function. Both of these functions can convert a sparse matrix from a file or a variable into a Matlab sparse matrix.

4. How do I calculate eigenvalues in Matlab?

To calculate eigenvalues in Matlab, you can use the eig function. This function takes in a square matrix as an input and outputs its eigenvalues, which represent the scalar values that satisfy the equation Ax = λx, where A is the matrix and λ is the eigenvalue.

5. Can I customize the output of eigenvalues in Matlab?

Yes, you can customize the output of eigenvalues in Matlab by using additional input arguments in the eig function. For example, you can specify the number of eigenvalues to be calculated or choose to output only the real or complex eigenvalues.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
820
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
2
Replies
41
Views
8K
  • Set Theory, Logic, Probability, Statistics
Replies
1
Views
732
  • Engineering and Comp Sci Homework Help
Replies
1
Views
984
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
9K
Back
Top