- “sparse” function: https://www.mathworks.com/help/matlab/ref/sparse.html
- “diag” function: https://www.mathworks.com/help/matlab/ref/diag.html
- “repmat” function: https://in.mathworks.com/help/matlab/ref/repmat.html
Unable to grasp the concept of Sparse Matrix - Kindly help me understand the following code.
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
% 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];
0 Kommentare
Antworten (1)
Arun
am 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))
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]
% Find the number of ones
numOnes = sum(S(:) == 1)
% Find the number of zeros
numZeros = sum(S(:) == 0)
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:
I hope this helps.
HTH
0 Kommentare
Siehe auch
Kategorien
Mehr zu Data Distribution Plots finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!