Interesting...I guess your question title should be edited to "use array as subscripts into multidimensional array". I dug into the sub2ind() to extract how to use subscripts to get a linear index and came up with this test
siz = [4,3,5,7]
subs = [1,2,4,6]
subsc = num2cell(subs)
ndx = sub2ind(siz,1,2,4,6)
ndx = sub2ind(siz,subsc{:})
ndx = subs(1);
k = cumprod(siz);
for i = 2:length(subs)
ndx = ndx + (subs(i)-1)*k(i-1);
end
ndx
So you should be able to do
function ndx = sub2ind_a(siz,subs)
ndx = subs(:,1);
k = cumprod(siz);
for i = 2:size(subs,2)
ndx = ndx + (subs(:,i)-1)*k(i-1);
end
end
And in your code:
siz = size(DATA);
a(cnt) = DATA(sub2ind_a(siz,idx));
For readability I would change your variable name "idx" to "subs_a" or something.
Best Answer