MATLAB: Variable Indexing for N-dimension data

indexindexingMATLABmatricessubscript

I am using Matlab R2013b. I've been struggling with Matlab indexing being a bottleneck in my functions. The issue keeps coming up. How can I tinker with Matlab's 'subsindex' function?
Overview of my problem: ——————————- I have a function that takes in a matrix of N-dimension. The function does not know the dimensions of the matrix beforehand. After some loops and things, the function must access data in the matrix. There's another function I have that creates a variable based on the number of inputs, essentially creating the same problem. I currently use 'sub2ind' to access or place values properly, but the 'sub2ind' function is slow. So slow it is a serious issue.
What I would Ideally like to do: —————————————— Let's say we have matrix A of N dimension. I have an index variable, indV. I want to be able to write A({indV}) without Matlab screaming an error at me about 'subsindex' not being defined for class 'cell'.
For example, if size(A) = (5, 4, 6) and indV=[1, 2, 3] then –> A({indV}) would be interpreted the same as A(1, 2, 3) resulting in one value. As you know matlab would produce three values if I wrote A(indV) which is seen as A(1:3). It would be a dream come true if I have the same A matrix and indV=[1 2] now and matlab would interpret A({indV},:) as A(1,2,:) resulting in 6 values instead of an error. What if indV1=3 and indV2=4 so A({indV1}, :, {indV2}) would be interpreted as A(3,:,4) producing 4 values, wouldn't that be nice. Even nicer would be with indV={2:4, 1} A({indV},:) would be seen as A(2:4, 1, :). You get the point.
I am hoping there is a way to modify/add to the 'subsindex' function to accomplish this resulting in a faster method than the 'sub2ind' function. Or maybe there is already functionality in Matlab that I am not aware of?
Any ideas/suggestions?

Best Answer

Or maybe there is already functionality in Matlab that I am not aware of?
I think so. If you have indV={1,2,3}, then A(indV{:}) is equivalent to A(1,2,3). See Comma Separated Lists.