I assume that "indices [x1 x2 ... xd]" mean indices along each dimension of an equivalent d-dimensional array.
You need to convert L and d to an array of dimensions, and then grab a few arguments from ind2sub
. Here is the function that does this. You can call it as x = myind2sub(L, d, i)
.
function out = myind2sub(L, d, ix) sz = repmat(L, [1 d]); %
But you should also ask why you store it in a linear array and compute indexes, instead of just storing it in a multidimensional array in the first place. In Matlab, a multidimensional array is stored in a contiguous block of memory, so it is efficient, and you can index it using either multidimensional indexes or linear indexing. If you have a linear array, just call reshape(myarray, sz)
to convert it to the multidimensional equivalent.
source share