Unable to grasp the concept of Sparse Matrix - Kindly help me understand the following code.

2 views (last 30 days)
% Sparse Matrix Indexes: S(i,j)=0 if dF(i)/dy(j)=0, else S(i,j)=1
diagonal = diag(ones(bins,1));
dcidci = diag(ones(bins,1),0)+diag(ones(bins-1,1),-1)+diag(ones(bins-1,1),+1);
dcidcj = diag(ones(bins,1))+diag(ones(bins-1,1),-1)+diag(ones(bins-2,1),-2);
dcdc = [dcidci dcidci dcidci; dcidci dcidci dcidci; dcidci dcidci dcidci];
dcdq = diag(ones(nspecies*bins,1));
dcdT = repmat(dcidci,[nspecies,1]);
dqdq = diag(ones(nspecies*bins,1));
dqdc = repmat(diagonal,nspecies);
dqdT = repmat(diagonal,[nspecies,1])
dTdc = repmat(dcidci,[1,nspecies]);
dTdq = repmat(diagonal,[1,nspecies]);
dTdT = dcidci;
S = [dcdc dcdq dcdT; dqdc dqdq dqdT; dTdc dTdq dTdT];

Answers (1)

Arun
Arun on 23 Feb 2024
Hi Hamza,
I understand that you want to some explanation about “sparse” matrix and the implementation of “sparse” matrix in the shared code using MATLAB.
A matrix is a two-dimensional data object, that represent data in the form of rows and columns. A “sparse” matrix is one where most of its elements are zero. Sparse matrix can be stored using less memory and require less computation time. MATLAB provides “sparse” function to convert a full matrix into sparse form by squeezing out any zero element.
The shared code utilizes mainly two functions, “diag” and “repmat”. The “diag” function, creates a diagonal matrix. The “repmat” function helps to create a matrix by repeating a given matrix in a specified number of times in both the row and column directions.
Since the code shared is missing some values, let us assume “bins” = 4 and “nspecies” = 3. I have removed commas to display the output in order to provide a better understanding of the code:
bins = 4;
nspecies = 3;
diagonal = diag(ones(bins,1))
diagonal = 4×4
1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1
dcidci = diag(ones(bins,1),0)+diag(ones(bins-1,1),-1)+diag(ones(bins-1,1),+1)
dcidci = 4×4
1 1 0 0 1 1 1 0 0 1 1 1 0 0 1 1
dcidcj = diag(ones(bins,1))+diag(ones(bins-1,1),-1)+diag(ones(bins-2,1),-2);
dcdc = [dcidci dcidci dcidci; dcidci dcidci dcidci; dcidci dcidci dcidci];
dcdq = diag(ones(nspecies*bins,1));
dcdT = repmat(dcidci,[nspecies,1])
dcdT = 12×4
1 1 0 0 1 1 1 0 0 1 1 1 0 0 1 1 1 1 0 0 1 1 1 0 0 1 1 1 0 0 1 1 1 1 0 0 1 1 1 0
dqdq = diag(ones(nspecies*bins,1));
dqdc = repmat(diagonal,nspecies);
dqdT = repmat(diagonal,[nspecies,1]);
dTdc = repmat(dcidci,[1,nspecies]);
dTdq = repmat(diagonal,[1,nspecies]);
dTdT = dcidci;
S = [dcdc dcdq dcdT; dqdc dqdq dqdT; dTdc dTdq dTdT]
S = 28×28
1 1 0 0 1 1 0 0 1 1 0 0 1 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 1 1 1 0 1 1 1 0 1 1 1 0 0 1 0 0 0 0 0 0 0 0 0 0 1 1 1 0 0 1 1 1 0 1 1 1 0 1 1 1 0 0 1 0 0 0 0 0 0 0 0 0 0 1 1 1 0 0 1 1 0 0 1 1 0 0 1 1 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 1 1 1 0 0 1 1 0 0 1 1 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 1 0 0 1 1 1 0 1 1 1 0 1 1 1 0 0 0 0 0 0 1 0 0 0 0 0 0 1 1 1 0 0 1 1 1 0 1 1 1 0 1 1 1 0 0 0 0 0 0 1 0 0 0 0 0 0 1 1 1 0 0 1 1 0 0 1 1 0 0 1 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 1 1 1 1 0 0 1 1 0 0 1 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 1 1 0 0 1 1 1 0 1 1 1 0 1 1 1 0 0 0 0 0 0 0 0 0 0 1 0 0 1 1 1 0
% Find the number of ones
numOnes = sum(S(:) == 1)
numOnes = 244
% Find the number of zeros
numZeros = sum(S(:) == 0)
numZeros = 540
As we can see that more that 68% of entries are zero element, “S” is an example of “sparse” matrix.
For more information, please refer to the shared documentation links:
  1. “sparse” function: https://www.mathworks.com/help/matlab/ref/sparse.html
  2. “diag” function: https://www.mathworks.com/help/matlab/ref/diag.html
  3. “repmat” function: https://in.mathworks.com/help/matlab/ref/repmat.html
I hope this helps.
HTH

Categories

Find more on Particle & Nuclear Physics in Help Center and File Exchange

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!