A matrix inequality with weights and its applications