Because you are trying to access some part of an array that does not exist. Just like this:
>> A = rand(4,3,2);
>> A(:,:,99)
??? Index exceeds matrix dimensions.
>>
My example array only has size 2 along the third dimension, so what do you expect to happen if I try to access the 99th position along the third dimension?
If you really want to learn how to write and debug your own code then one very important skill is to start actually looking at your code. You could have very easily figured this error out yourself by looking at each of the variables, and trying each part of that line: break it down into atomic operations, and trying them in the command line. And by reading the error message of course.