Susbtracting elements of a column with every other element but in a particular order without repeating same susbtraction in reverse order.
Show older comments
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.
Accepted Answer
More Answers (1)
Walter Roberson
on 8 Jul 2021
0 votes
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.
Categories
Find more on Random Number Generation in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!