1

I have a 2-D array. And I want to access it with rows and colums index stored in another 2-D array. Example: Now I don't want to use loops but I want to access A(1, 2) and A(3, 4).

A = ones(10,10)
B = [1, 2 ; 3, 4]

If I do A(b(:,1), b(:,2)), this will result in all possible combination of [1,2] and [3,4]. How can it be done?

1 Answers1

1

Use MATLAB's sub2ind function:

A(sub2ind(size(A),B(:,1),B(:,2)))
ibezito
  • 5,782
  • 2
  • 22
  • 46