Retrieving elements from a matrix