You mentioned that your code is working fine for I=0 but when I tried with I=0, I got the error:
Unable to perform assignment because the size of the left side is 0-by-0 and the size of the right side is 10-by-10
for the line:
When I is not equal to 0, the determination of positive definiteness depends on the values provided as input. Try manually checking whether the matrix you obtain is positive semi definite or not.