Assign a set of values in a matrix depending on the index of it
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
etudiant_is
el 18 de Mzo. de 2016
Respondida: etudiant_is
el 19 de Mzo. de 2016
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?
1 comentario
Respuesta aceptada
Más respuestas (1)
Image Analyst
el 18 de Mzo. de 2016
You didn't really say what you're doing to get D. So what I did, using pdist2() in the Statistics and Machine Learning Toolbox, is
P1 = [12 45
34 7 ]
P2 = [2 42 25
6 37 57
3 55 16]
distances = pdist2(P1, P2(:, 2:end))
I assume you did the same. So I got
distances =
36.0555127546399 27.7308492477241 51.8652099195598
19.6977156035922 50.0899191454728 22.8473193175917
The 2 rows correspond to the 2 points from P1, and the 3 columns are the distances to the corresponding 3 points in P2. But what I don't know is what you want Dall to look like.
3 comentarios
Image Analyst
el 18 de Mzo. de 2016
Sorry, but we can't figure out the "rule" for how you distribute the numbers from distances into Dall.
Ver también
Categorías
Más información sobre Matrix Indexing en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!