fork download
  1. @(d,i)sub2ind(flip(d),num2cell(flip(i+1)){:})-1
  2.  
  3. % to run it:
  4. f=ans;
  5.  
  6. % Dimensions: [5, 10]
  7. % Indices: [4, 2]
  8. % Output: 42
  9. f([5, 10], [4, 2])
  10.  
  11. % Dimensions: [10, 10, 4, 62, 7]
  12. % Indices: [1, 2, 3, 4, 5]
  13. % Output: 22167
  14. f([10, 10, 4, 62, 7], [1, 2, 3, 4, 5])
  15.  
  16. % Dimensions: [5, 1, 10]
  17. % Indices: [3, 0, 7]
  18. % Output: 37
  19. f([5, 1, 10], [3, 0, 7])
  20.  
  21. % Dimensions: [6, 6, 6, 6, 6, 6, 6, 6, 6, 6]
  22. % Indices: [3, 1, 5, 5, 3, 0, 5, 2, 5, 4]
  23. % Output: 33570178
  24. f([6, 6, 6, 6, 6, 6, 6, 6, 6, 6], [3, 1, 5, 5, 3, 0, 5, 2, 5, 4])
Success #stdin #stdout 0.47s 121792KB
stdin
Standard input is empty
stdout
ans =

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

ans =  42
ans =  22167
ans =  37
ans =  33570178