diff --git a/sPuReMD/src/lin_alg.c b/sPuReMD/src/lin_alg.c
index a521737d382e9807c0ba6e1a405d9b84e7199557..6d135cd3d86a08515023b0835814d8636368a850 100644
--- a/sPuReMD/src/lin_alg.c
+++ b/sPuReMD/src/lin_alg.c
@@ -1702,7 +1702,7 @@ real Sparse_Approx_Inverse( const sparse_matrix * const A,
             // all rows are initialized to zero
             for ( d_j = 0; d_j < N; ++d_j )
             {
-                dense_matrix[d_i * M + d_j] = 0.0;
+                dense_matrix[d_i * N + d_j] = 0.0;
             }
 
             // change the value if any of the column indices is seen
@@ -1711,7 +1711,7 @@ real Sparse_Approx_Inverse( const sparse_matrix * const A,
             {
                 if ( Y[A_full->j[d_j]] == 1 )
                 {
-                    dense_matrix[d_i * M + pos_y[d_j]] = A_full->val[d_j];
+                    dense_matrix[d_i * N + pos_y[d_j]] = A_full->val[d_j];
                 }
             }
         }