How do i get the compressed sparse row for the following matrix ?

9 ビュー (過去 30 日間)
De Silva
De Silva 2021 年 10 月 12 日
回答済み: Vidhi Agarwal 2024 年 2 月 29 日
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

回答 (1 件)

Vidhi Agarwal
Vidhi Agarwal 2024 年 2 月 29 日
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.

カテゴリ

Help Center および File ExchangeLarge Files and Big Data についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by