Return values for specific set of indices

3 views (last 30 days)
I am trying to find values in a matrix for specific indices. I have a 180x180 matrix, N, and have a range of dx and dy values. How do I scan matrix N to find values for which the combination of [i,j] coordinate in the matrix is the [dx,dy] pair values? For example, if dx(3) = 5 and dy(3) = 10, I want to return the matrix value at i = 5, j = 10. Or if I have dx(2) = 1, dy(2) = 5, I want to return the matrix value at i = 1 and j = 5. I want to do this over the entire range of dx and dy.
dx = [0 1 3 10 15 12 4 7];
dy = [1 4 62 1 33 4 10 22];
N11 = randi([0,5],45,45);
N12 = randi([1,4],45,45);
N13 = randi([0,7],45,45);
N14 = randi([0,6],45,45);
N21 = randi([1,3],45,45);
N22 = randi([1,7],45,45);
N23 = randi([4,6],45,45);
N24 = randi([2,7],45,45);
N31 = randi([1,4],45,45);
N32 = randi([6,7],45,45);
N33 = randi([1,7],45,45);
N34 = randi([3,4],45,45);
N41 = randi([2,5],45,45);
N42 = randi([0,3],45,45);
N43 = randi([5,6],45,45);
N44 = randi([0,4],45,45);
N = [N11 N12 N13 N14;
N21 N22 N23 N24;
N31 N32 N33 N34;
N41 N42 N43 N44];

Accepted Answer

Andrei Bobrov
Andrei Bobrov on 18 Apr 2017
out = N(sub2ind(size(N),dx, dy));

More Answers (0)

Categories

Find more on Matrices and Arrays in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!