MATLAB: Index a multi-dimensional cell array with a vector

cell arraysindexingvector

I have a cell-array with N dimensions:
x=cell(ones(1,N));
Now, I'd like to be able to access elements on x using vector as index:
%Assuming N=5
V=[1 4 5 3 1]; %Those are coordinates that I need in all 5 dimensions
z = x(V) % that should give me the element
or another way around
x(V) = z %That should assign z to the same cell where I got it from
Now the problem is that Matlab does not allow that. Instead it would give me the vector with elements numbered as in V, but through linear indexing. Is there a way to work around that? I'd assume same question is true for arrays, because if you do not know dimensionality in advance, you'll need to use linear indexing which is quite cumbersome.
Thank you, Alex.

Best Answer

Accepting vectors:
function Index = sub2indV(X, V)
k = [1, cumprod(size(X))];
Index = sum(k(1:length(V)) .* (V - 1)) + 1;