0

Say I have:

M = [1, 2; 3, 4]
v = [1, 2];

M(v) gives [1 3] but I want to get is M(1, 2) = 3.

Is there a way to do it? Of course, I could do M(v(1), v(2)) but I need the thing to work for M an N-dimensional array and v a vector of length N.

Thanks very much

Eitan T
  • 32,660
  • 14
  • 72
  • 109
JuliaR
  • 99
  • 5

2 Answers2

1

If your goal is to get values from M, I would use M(sub2ind(size(M),v(:,1),v(:,2))) this will work for vector version of v nicely.

emoberg
  • 11
  • 1
0

You could use how Matlab expands cell

M = [1, 2; 3, 4]
v = {1, 2}
M( v{:} )

( you can transform v with v_cell=num2cell(v) )

bdecaf
  • 4,652
  • 23
  • 44