How do i get the compressed sparse row for the following matrix ?
14 views (last 30 days)
Show older comments
How can i get the compressed sparse row for the folling matrix ?
I am trying to solve a 14 bus Power Flow real life problem. testing a 5x5 matrix first to see if it will work.
So far i have the code given below.
A=[ 1 0 -2 0 0;...
2 8 0 1 0;...
0 0 3 0 -2;...
0 -3 2 0 0;...
1 2 0 0 -4]
n=length(A);
c=1;
for i=1:n
for j=1:n
if A(i,j) ~= 0;
row(c,1)=i;
col(c,1)=j;
val(c,1)=A(i,j);
index(c,1)= c;
cz=[index val];
c=c+1;
end
end
end
0 Comments
Answers (1)
Vidhi Agarwal
on 29 Feb 2024
Hi De Silva,
I understand you have a query regarding creating a CSR for a given matrix.
Compressed Sparse Row (CSR) is a way to store sparse matrices, where most elements are zero, using less memory. It uses three arrays:
Values Array: Holds all non-zero elements in order.
Column Indices Array: Has the column positions for each non-zero element.
Row Pointers Array: Shows where each row's data starts in the Values Array, with one extra element at the end as a marker.
Following is the approch to do the same.
A = [ 1 0 -2 0 0;...
2 8 0 1 0;...
0 0 3 0 -2;...
0 -3 2 0 0;...
1 2 0 0 -4];
% Initialize the arrays
values = [];
col_indices = [];
row_pointers = [1]; % The first row always starts at index 1
for i = 1:size(A,1)
for j = 1:size(A,2)
if A(i,j) ~= 0
values(end+1) = A(i,j);
col_indices(end+1) = j;
end
end
row_pointers(end+1) = length(values) + 1;
end
row_pointers(end) = [];
And than you can display the Values array, Column indices array and Row Pointers array.
0 Comments
See Also
Categories
Find more on Large Files and Big Data in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!