Phase transitions and sample complexity in Bayes-optimal matrix factorization