# Append rows and columns of zeros to sparse matrix, but only if they don't already exist

## Alec Jacobson

## September 03, 2015

Analogous to the trick for dense matrices, here's a way to add more empty rows and columns to sparse matrix, but only if they don't already exist. This happens to me often when dealing with meshes that might have unreferenced vertices. For example, if I build the adjacency matrix of a mesh (V,F):

```
A = adjacency_matrix(F);
```

the size will be max(F(:)) by max(F(:)), so if there's an unreferenced vertex with index greater than max(F(:)) I'll run into trouble. A rather verbose inelegant way to solve this is:

```
n = size(V,1);
A = [A sparse(size(A,1),n - size(A,2));sparse(n - size(A,1),n)];
```

But here's a cute way to do it with far fewer characters:

```
A(end+1:n,end+1:n) = 0;
```