I'm guessing that the NaN thing is a red herring -- I think that's just where the error happens to be thrown (ie some general checking algorithm behind the scenes that looks for NaNs).
The real problem is that MATLAB is a bit idiosyncratic with the way it deals with multidimensional arrays. You have 3-D arrays which you're indexing into... that's all well and good, except that the result isn't a vector like you might expect:
A = randi(5,3,2,4)
A(2,1,:)
Note that the output is a 1-by-1-by-4 array. Although that's 1-dimensional, MATLAB sees it still as a 3-D array. The solution is to squeeze it:
squeeze(A(2,1,:))