Kid_A
Kid_A

Reputation: 47

Matlab Question on Sparse Matrices

I have a sparse matrix S. I perform the following operation D1 = diag(sum(S,2)), basically forming a diagonal matrix. Now I need to perform (D1)^(-0.5), but I get an error "Error using mpower, use full(x)^full(y)"

Converting to full will defeat the purpose of using a sparse matrix.

Any advice will be very helpful.

Upvotes: 3

Views: 537

Answers (1)

Latanius
Latanius

Reputation: 2653

Raising a diagonal matrix to a power can be done simply by doing the operation on the diagonal elements elementwise... so:

D1_diagonal_elements = sum(S,2);
your_result = diag(D1_diagonal_elements .^ (-0.5));

Upvotes: 3

Related Questions