- “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 visualización (últimos 30 días)
Mostrar comentarios más antiguos
% 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 comentarios
Respuestas (1)
Arun
el 23 de Feb. de 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 comentarios
Ver también
Categorías
Más información sobre Data Distribution Plots en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!