A generalization of Sylvester's identity on determinants and some applications