Efficient way to do elementry operations on sparse matrix

1 vue (au cours des 30 derniers jours)
Orr Streicher
Orr Streicher le 11 Mar 2021
Commenté : Orr Streicher le 11 Mar 2021
Hi,
I am working with sparse matrices and i wonder if there is an efficient way to scan the elements of this kind of matrices and to perform some elemtry operations on them.
To be more specific, I have a sparse matrix W with elements w_ij. I want to create another sparse matrix Q in the same size of W where its elemets are q_ij=(w_ij^2)/[(w_ii^2)(w_jj^2)]. (i know that the diagonal elements of W are not zeros)
I wonder if there is an option to scan and perform this operation only on the exsit elements of W (and not to scan all its elements since most of them are zeros )
Thanks

Réponse acceptée

Walter Roberson
Walter Roberson le 11 Mar 2021
You can use the three-output form of find():
[row, column, value] = find(W);
diags = full(diag(W)); %we are told diagonals are non-zero so this is dense
Q = sparse(row, column, value.^2 ./ (diags(row).^2 .* diag(column).^2));

Plus de réponses (0)

Catégories

En savoir plus sur Sparse Matrices dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by