MATLAB: Assign a set of values in a matrix depending on the index of it

index of elementno loop

I have two matrices from which I compute the distances between Points in P1 and P2. In P1, I have only X and Y coordinates.
P1=[12 45
34 7 ]
and in P2 , I have the index of the point (in the first column), X and Y coordinates. The index in the first column is not sorted and it is not complete. Like in the example, I don't have indices 1,4 and 5.
P2= [2 42 25
6 37 57
3 55 16]
So after computing the distances bw the points of P1 and P2, I have the result in another matrix D. Now, I have another matrix Dall, where I have rows for all points (from P2 and those not also into it). I want to put the distances I got from D inside of Dall for the corresponding rows.
What I am doing with a for loop is
for i=1:size(P1,1)
for j=1:size(P2,1)
x=P2(i,1);
Dall(x,j) = D(i,j);
end
end
Any more optimal way to do it?

Best Answer

What worked for me is
Dall(P2(:,1),:)= D
Related Question