kmarx
kmarx

Reputation: 15

Extract list of values from array given coordinate list

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'.

Upvotes: 0

Views: 268

Answers (4)

Tom
Tom

Reputation: 445

Yet another way:

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

Upvotes: 1

SimplyDanny
SimplyDanny

Reputation: 186

Another way:

A(sub2ind(size(A), coord(:, 1), coord(:, 2)))

Upvotes: 2

Tom
Tom

Reputation: 445

Can you try the following:

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

Upvotes: 0

Puff
Puff

Reputation: 515

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.

Upvotes: 0

Related Questions