@(d,i)sub2ind(flip(d),num2cell(flip(i+1)){:})-1

% to run it:
f=ans;

% Dimensions: [5, 10]
% Indices: [4, 2]
% Output: 42
f([5, 10], [4, 2])

% Dimensions: [10, 10, 4, 62, 7]
% Indices: [1, 2, 3, 4, 5]
% Output: 22167
f([10, 10, 4, 62, 7], [1, 2, 3, 4, 5])

% Dimensions: [5, 1, 10]
% Indices: [3, 0, 7]
% Output: 37
f([5, 1, 10], [3, 0, 7])

% Dimensions: [6, 6, 6, 6, 6, 6, 6, 6, 6, 6]
% Indices: [3, 1, 5, 5, 3, 0, 5, 2, 5, 4]
% Output: 33570178
f([6, 6, 6, 6, 6, 6, 6, 6, 6, 6], [3, 1, 5, 5, 3, 0, 5, 2, 5, 4])