Susbtracting elements of a column with every other element but in a particular order without repeating same susbtraction in reverse order.

2 visualizaciones (últimos 30 días)
Hello, I am having trouble in susbtracting elements from column, let's say I have this elements
-2.35007=A1
-8.61753=A2
-7.09735=A3
0.221006=A4
0.604873=A5
So I have 5 elements here in this row. Let's assume these are position co-ordinates of different particles in any given space. I want to create pair of each particle with every other particle in the space. To do so I have to find the displacement vector of each pair. 5 particles would form 5c2 pairs i.e 5*4/2 i,e 10 pairs. That would be (A1-A2), (A1-A3),( A1-A4), (A1-A5), (A2-A3),(A2-A4),(A2-A5),(A3-A4),(A3-A5),(A4-A5). I don't want repeating susbtraction like A3-A1 or A5-A4, I want the elements to be substracted from top to bottom or from bottom to top. It should be one way not both ways.
If its possible to do via for loop please post it. Help would be greatly appreciated.

Respuesta aceptada

Soniya Jain
Soniya Jain el 7 de Jul. de 2021
Hey you can store the values A1, A2,.. in a vector, i hope you find the below code useful,
A(1) = -2.35007;
A(2) = -8.61753;
A(3) = -7.09735;
A(4) = 0.221006;
A(5) = 0.604873;
k = 1;
for i=1:4
for j=i+1:5
D(k) = A(i)-A(j);
k = k+1;
end
end
D will be 1X10 vector containing the difference value of all the combinations.

Más respuestas (1)

Walter Roberson
Walter Roberson el 8 de Jul. de 2021
If you have the Statistics and Machine Learning Toolbox, you can use pdist(), which is designed for finding the distance of each point to each other point, and it only calculates in triangle form, as you require.
Could you confirm that you want signed displacement, not unsigned displacement? So (A1-A3) as a possibly negative value, not abs(A1-A3) or sqrt( sum((A1-A3).^2) ) ? If so then you will need to specify a custom distance function for pdist(), as pdist() is designed for distance and distances are never negative.

Categorías

Más información sobre Genomics and Next Generation Sequencing 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!

Translated by