Solution to matrix exercise