Fix segfault in sparse matrices