Efficient way to do elementry operations on sparse matrix

1 view (last 30 days)
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

Accepted Answer

Walter Roberson
Walter Roberson on 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));

More Answers (0)

Categories

Find more on Sparse Matrices 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!