MATLAB: Find all possible combinations of 2 vectores (spin) of a system with N atoms with pondered distance

loopssave

Hello!
I wanna take a tridimensional system of atoms, calculate the distance between them and all possible combinations of spin.
I want to take note that with the and being the magnetic moment of i and j repectivelly, which ones could take the values -1 and 1 and the values of 1's must be equal to the number of -1's. Thus, i want to take note of the value of and for each atom and for all different values of the sum.
Hope, you can help me fixing this problem. I leave what I have attached.
Thanks

Best Answer

You have to bear in mind that most of us probably don't work in the same field as you do. Last time I had to worry about atom spin was some 20 years ago... So make sure to explain yourself clearly. Giving us code with syntax errors, or matrices that are not valid syntax does not help.
I want to calculate the distance between each atom,
I assume euclidean distance. That's trivially done, in just one line.
A=[ 0.000000000 0.000000000 0.000000000
0.000000000 0.000000000 2.700000048
0.000000000 2.700000048 0.000000000
0.000000000 2.700000048 2.700000048
2.700000048 0.000000000 0.000000000
2.700000048 0.000000000 2.700000048
2.700000048 2.700000048 0.000000000
2.700000048 2.700000048 2.700000048
1.350000024 1.350000024 1.350000024
1.350000024 1.350000024 4.050000191
1.350000024 4.050000191 1.350000024
1.350000024 4.050000191 4.050000191
4.050000191 1.350000024 1.350000024
4.050000191 1.350000024 4.050000191
4.050000191 4.050000191 1.350000024
4.050000191 4.050000191 4.050000191];
distance = sqrt(sum((permute(A, [1 3 2]) - permute(A, [3 1 2])) .^ 2, 3))
assign the values and 1 (spin) to each atom
That;s really not clear. Going by your +- diagram, I think you want to get all possible combinations of 8 atoms out of 16 (once you've chosen 8 positive spins, the others are negative). That's trivially done again however note that for 16 atoms, there are 12870 such combinations. For more atoms, it will easily get out of hands:
ncombs = nchoosek(size(A, 1), size(A, 1)/2); %number of combinations. If it's too large the following line will take forever and may even lock your computer
pospin = nchoosek(1:size(A, 1), size(A, 1)/2); %each row is a unique combination of 8 out 16 atoms
We can fill a matrix of -1 and +1 easily from there:
spin = -ones(size(pospin, 1), size(A, 1)); %fill with negative spin
spin(sub2ind(size(spin), repmat((1:size(spin, 1))', 1, size(pospin, 2)), pospin)) = 1; %fill selected atoms with positive spin
calculate all possible combinations of these values.
What values? Again, taking a guess based on your equation:
%using dimensional notation introduced in R2018b, here.
%in earlier versions, you'd have to call sum twice.
result = squeeze(sum(permute(spin, [3 2 1]) .* permute(spin, [2 3 1]) .* distance, [1 2]))