I think you may be misunderstanding the nature of gimbal lock. It sounds like you may be trying to remove an actual term in a rotation matrix calculation , but this is incorrect because each axis is still able to rotate. What happens with gimbal lock is that one of the rotational degrees of freedom of the object you are rotating is removed. This happens when two rotational axes become aligned.
In the image on the left, pins that allow the blue and orange rings to rotate are aligned. This means that the blue ring may rotate to allow pitch, the green ring may rotate to allow yaw, but the orange ring does not allow roll. The orange ring is aligned with the blue ring, meaning that it also can only allow pitch motion.
The way to achieve roll is to separate the blue and orange rings, as shown on the right image above. To pitch the orange ring moves, and then yaw and roll are movements of the blue and green rings together.
Notice again that, even in gimbal lock, each ring (each axis) can still move - no term would be removed from a rotation matrix equation. Instead, the object is "locked" on the roll axis because the physical arrangement is such that there is no way to roll.