Extract list of values from array given coordinate list

249 views Asked by At

I have a large 2D matrix, A (8192x10201) and a list of coordinates, coord (3622x2). I am trying to find the value of the array at each index and put it into a 1D list.

I could use the following for loop, but I was wondering if there was a more elegant solution.

data = [];
for ii = 1:numel(coord(:,1))
    data = [data; A(coord(ii,1), coord(ii,2)];
end

EDIT: Things I have tested:

1.data = A(coord)

data is a 3622x2 matrix. I'm not certain how the values in data relate to the coordinates in coord.

2.data = A(coord(:,1), coord(:,2))

data is 3622x3622 matrix. I am very much uncertain of how it relates to 'coord'.

4

There are 4 answers

0
SimplyDanny On BEST ANSWER

Another way:

A(sub2ind(size(A), coord(:, 1), coord(:, 2)))
0
Puff On

I'd think data = A(coord) should do it. If not, data = A(coord(:,1),cord(:,2)) surely will. I can't test right now, so something might me slipping my mind.

4
Tom On

Can you try the following:

diag(A(coord(:,1),coord(:,2)))
1
Tom On

Yet another way:

A(coord(:, 1) + (coord(:, 2)-1)*size(A,1))