When I generate a dynamical matrix, I have a bunch of 0's after several lines to the end. Would anyone give me the possible reasons? Is the end part of the matrix file related to the displaced ions in the end part of POSCAR?

I think the issue here is probably that there are fewer force calls in the OUTCAR file than degrees of freedom in the DISPLACECAR file. The NSW variable should equal to the number of degrees of freedom plus one. If vasp terminates early, or if this was miscounted, there will be fewer force calls in the OUTCAR than are needed to general the full dynamical matrix. In this case, the final lines of the matrix will contain only zeros.

It can also be that the NSW can be higher than the number of degrees of freedom. When all lines in the DISPLACECAR have been exhausted then the system doesn't move and the forces stay the same after that. So when the matrix is set up with finite difference of the force, zero values are obtained.