I presume the "~-0" clause is to not eliminate i==j? Or can there be actual identical matches elsewhere?
Either way the first loop
All the other variables are the same as HS_elim, just copies of the same thing. Not sure why???
But, now you have indices, not values you have to look up again -- just set those locations:
LengthofTC4 = size(HS_elim,1);
for j = 1:LengthofTC4
x(HS_elim_x(j)) = NaN;
y(HS_elim_y(j)) = NaN;
if you still want it to be on element-by-element basis -- but unless you pause() between you probably can't see the evolution anyway, so simply
x(HS_elim_x) = NaN;
y(HS_elim_y) = NaN;
does the dirty...
The above does assume there aren't zeros elsewhere other than the diagonal...that's the point of the Inf() on the diagonal of the squareform array...if there are other locations, you can, of course, write a compound expression for the find() operation; I was just trying to do it with one by eliminating zero elements first.