Borrar filtros
Borrar filtros

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

8 visualizaciones (últimos 30 días)
De Silva
De Silva el 12 de Oct. de 2021
Respondida: Vidhi Agarwal el 29 de Feb. de 2024
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

Respuestas (1)

Vidhi Agarwal
Vidhi Agarwal el 29 de Feb. de 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.

Categorías

Más información sobre Large Files and Big Data 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!

Translated by