필터 지우기
필터 지우기

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

조회 수: 8 (최근 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 CenterFile Exchange에서 Large 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