Tests for existence of matrix before entering shell loop.
Tests for existence of expression before attempting to write or optimise.