You have the expression
p=((-1)^j)*(factorial(M)/factorial(j)*factorial(M-j))*exp(-(g.*y(1-(1/j))));
The y(1-(1/j)) part of that says to index the variable y at index (1-(1/j)) . As all of your j are at least 2, 1/j is less than 1 so 1-1/j is not a positive integer.
Perhaps you wanted to code multiplication instead of indexing. y*(1-(1/j))