I need help with calculations with condition for a matrix