
- MATLAB - Home
- MATLAB - Overview
- MATLAB - Features
- MATLAB - Environment Setup
- MATLAB - Editors
- MATLAB - Online
- MATLAB - Workspace
- MATLAB - Syntax
- MATLAB - Variables
- MATLAB - Commands
- MATLAB - Data Types
- MATLAB - Operators
- MATLAB - Dates and Time
- MATLAB - Numbers
- MATLAB - Random Numbers
- MATLAB - Strings and Characters
- MATLAB - Text Formatting
- MATLAB - Timetables
- MATLAB - M-Files
- MATLAB - Colon Notation
- MATLAB - Data Import
- MATLAB - Data Output
- MATLAB - Normalize Data
- MATLAB - Predefined Variables
- MATLAB - Decision Making
- MATLAB - Decisions
- MATLAB - If End Statement
- MATLAB - If Else Statement
- MATLAB - If…Elseif Else Statement
- MATLAB - Nest If Statememt
- MATLAB - Switch Statement
- MATLAB - Nested Switch
- MATLAB - Loops
- MATLAB - Loops
- MATLAB - For Loop
- MATLAB - While Loop
- MATLAB - Nested Loops
- MATLAB - Break Statement
- MATLAB - Continue Statement
- MATLAB - End Statement
- MATLAB - Arrays
- MATLAB - Arrays
- MATLAB - Vectors
- MATLAB - Transpose Operator
- MATLAB - Array Indexing
- MATLAB - Multi-Dimensional Array
- MATLAB - Compatible Arrays
- MATLAB - Categorical Arrays
- MATLAB - Cell Arrays
- MATLAB - Matrix
- MATLAB - Sparse Matrix
- MATLAB - Tables
- MATLAB - Structures
- MATLAB - Array Multiplication
- MATLAB - Array Division
- MATLAB - Array Functions
- MATLAB - Functions
- MATLAB - Functions
- MATLAB - Function Arguments
- MATLAB - Anonymous Functions
- MATLAB - Nested Functions
- MATLAB - Return Statement
- MATLAB - Void Function
- MATLAB - Local Functions
- MATLAB - Global Variables
- MATLAB - Function Handles
- MATLAB - Filter Function
- MATLAB - Factorial
- MATLAB - Private Functions
- MATLAB - Sub-functions
- MATLAB - Recursive Functions
- MATLAB - Function Precedence Order
- MATLAB - Map Function
- MATLAB - Mean Function
- MATLAB - End Function
- MATLAB - Error Handling
- MATLAB - Error Handling
- MATLAB - Try...Catch statement
- MATLAB - Debugging
- MATLAB - Plotting
- MATLAB - Plotting
- MATLAB - Plot Arrays
- MATLAB - Plot Vectors
- MATLAB - Bar Graph
- MATLAB - Histograms
- MATLAB - Graphics
- MATLAB - 2D Line Plot
- MATLAB - 3D Plots
- MATLAB - Formatting a Plot
- MATLAB - Logarithmic Axes Plots
- MATLAB - Plotting Error Bars
- MATLAB - Plot a 3D Contour
- MATLAB - Polar Plots
- MATLAB - Scatter Plots
- MATLAB - Plot Expression or Function
- MATLAB - Draw Rectangle
- MATLAB - Plot Spectrogram
- MATLAB - Plot Mesh Surface
- MATLAB - Plot Sine Wave
- MATLAB - Interpolation
- MATLAB - Interpolation
- MATLAB - Linear Interpolation
- MATLAB - 2D Array Interpolation
- MATLAB - 3D Array Interpolation
- MATLAB - Polynomials
- MATLAB - Polynomials
- MATLAB - Polynomial Addition
- MATLAB - Polynomial Multiplication
- MATLAB - Polynomial Division
- MATLAB - Derivatives of Polynomials
- MATLAB - Transformation
- MATLAB - Transforms
- MATLAB - Laplace Transform
- MATLAB - Laplacian Filter
- MATLAB - Laplacian of Gaussian Filter
- MATLAB - Inverse Fourier transform
- MATLAB - Fourier Transform
- MATLAB - Fast Fourier Transform
- MATLAB - 2-D Inverse Cosine Transform
- MATLAB - Add Legend to Axes
- MATLAB - Object Oriented
- MATLAB - Object Oriented Programming
- MATLAB - Classes and Object
- MATLAB - Functions Overloading
- MATLAB - Operator Overloading
- MATLAB - User-Defined Classes
- MATLAB - Copy Objects
- MATLAB - Algebra
- MATLAB - Linear Algebra
- MATLAB - Gauss Elimination
- MATLAB - Gauss-Jordan Elimination
- MATLAB - Reduced Row Echelon Form
- MATLAB - Eigenvalues and Eigenvectors
- MATLAB - Integration
- MATLAB - Integration
- MATLAB - Double Integral
- MATLAB - Trapezoidal Rule
- MATLAB - Simpson's Rule
- MATLAB - Miscellenous
- MATLAB - Calculus
- MATLAB - Differential
- MATLAB - Inverse of Matrix
- MATLAB - GNU Octave
- MATLAB - Simulink
MATLAB - Sparse Matrix
A matrix is a two-dimensional array of numbers, symbols, or variables arranged in rows and columns. The dimensions of a matrix are given as "m x n," where "m" represents the number of rows, and "n" represents the number of columns.The individual elements of the matrix are usually referred to by their row and column indices, denoted as "a," where "i" indicates the row number and "j" indicates the column number.
Example
Here A is a 2x3 matrix i.e. it has 2 rows and 3 columns.
A = 1 2 3 4 5 6
Here A11 = 1 , A12 = 2 , A13 = 3. A21 = 4, A22 = 5 and A23 = 6.
What is a Sparse Matrix?
Sparse matrices are a type of matrix in which the majority of the elements are zero. So you will see a lot of zero elements in a sparse matrix in comparison to non-zero elements.
Why do we need Sparse Matrices?
Here are following benefits that we get from using sparse matrix −
Advantages of Sparse Matrices
- Efficient Memory Usage − Sparse matrices are advantageous in terms of memory usage. Since most of the elements are zero, storing only the non-zero values can save a significant amount of memory compared to storing a dense matrix, which includes a large number of unnecessary zero values.
- Computational Efficiency − Many operations involving sparse matrices can be optimized to take advantage of the sparsity structure. This leads to faster computations compared to dense matrices, where calculations involving a large number of zero values can be wasteful.
- Reduced Complexity − Algorithms that operate on sparse matrices often exhibit lower complexity due to the reduced number of non-zero elements. This can result in faster algorithms and reduced processing time.
- Solving Sparse Linear Systems − Sparse matrices frequently arise in systems of linear equations, and specialized techniques like iterative solver can be more efficient for solving sparse linear systems than direct methods used for dense matrices.
Disadvantages of Sparse Matrices
- Complex Indexing − Operations involving sparse matrices often require more complex indexing and data structures to efficiently navigate and manipulate the non-zero elements. This complexity can make the implementation of algorithms involving sparse matrices more challenging.
- Sparse Matrix-Vector Multiplication − While certain operations can be highly efficient with sparse matrices, matrix-vector multiplication can be less efficient compared to dense matrices, especially when the sparsity pattern is irregular.
Creation of Sparse Matrix
In MATLAB, you can create a sparse matrix using the sparse function. This function allows you to specify the non-zero values and their corresponding row and column indices. The sparse function automatically converts the non-zero elements of the dense matrix into a sparse format while omitting the zeros.
Syntax
S = sparse(A)
Here A is the dense matrix from which you want to create the sparse matrix.In a dense matrix, the majority or all of its elements are non-zero.
Let us create a sparse matrix from a dense matrix.
Example 1
A = [0, 0, 0, 0;0, 1, 0, 2; 0, 0, 3, 0;0, 4, 0, 5] sparse_matrix = sparse(A)
In this example, A is the input dense matrix, and sparse_matrix is the resulting sparse matrix created using the sparse function. The function automatically extracts the non-zero values and their corresponding row and column indices from the dense matrix to create the sparse representation.
When you execute the same in Matlab the output is as follows −
>> A = [0, 0, 0, 0;0, 1, 0, 2; 0, 0, 3, 0;0, 4, 0, 5] >> sparse_matrix = sparse(A) A = 0 0 0 0 0 1 0 2 0 0 3 0 0 4 0 5 sparse_matrix = Compressed Column Sparse (rows = 4, cols = 4, nnz = 5 [31%]) (2, 2) -> 1 (4, 2) -> 4 (3, 3) -> 3 (2, 4) -> 2 (4, 4) -> 5 >>
Sparse Matrix of Nonzeros with Specified Size
Creating a sparse matrix of nonzeros with a specified size involves providing the dimensions of the matrix along with the non-zero elements' locations and values.
Example
num_rows = 5; num_cols = 5; nonzero_rows = [1, 2, 3, 4] nonzero_cols = [2, 4, 1, 3] nonzero_vals = [10, 20, 30, 40] sparse_matrix = sparse(nonzero_rows, nonzero_cols, nonzero_vals, num_rows, num_cols)
In the example above, the sparse matrix created will have dimensions 5x5. It will have non-zero elements at positions (1,2), (2,4), (3,1), and (4,3), with corresponding values 10, 20, 30, and 40.
When you execute above example in matlab command window the output is −
>> num_rows = 5; num_cols = 5; nonzero_rows = [1, 2, 3, 4] nonzero_cols = [2, 4, 1, 3] nonzero_vals = [10, 20, 30, 40] sparse_matrix = sparse(nonzero_rows, nonzero_cols, nonzero_vals, num_rows, num_cols) nonzero_rows = 1 2 3 4 nonzero_cols = 2 4 1 3 nonzero_vals = 10 20 30 40 sparse_matrix = Compressed Column Sparse (rows = 5, cols = 5, nnz = 4 [16%]) (3, 1) -> 30 (1, 2) -> 10 (4, 3) -> 40 (2, 4) -> 20 >>