Data Linking And value reassignment for variables

Hello, I need to reassign selected plotted points to NaN values and update my graph so that they no longer show up on the graph but I am having trouble getting my code to work. I need to compare x and y values between 2 different sized arrays. If the coordinates compared match I need those coordinates to be sent back to the original matrix as NaN values and then have the current figure updated accordingly and those points no longer showing on the graph. Below is the string of code I am working with. Any help would be greatly appreciated. X and Y are the where the orignial points are stored while HS_elim is the array that contains the points that should be eliminated.
LengthofTC4 = length(HS_elim);
for i = 1:n
for j = 1:LengthofTC4
if x(i) == HS_elim(j,1) || y(i) == HS_elim(j,2)
x(i) = NaN;
y(i) = NaN;
refreshdata(CTR)
drawnow
end
end
end

7 Comments

dpb
dpb on 12 Aug 2019
Edited: dpb on 12 Aug 2019
Is HS_elim values of x or indices somehow returned?
Seeing a (smallish) example of your actual data arrays would undoubtedly clarify...
Also note, if is trying to find values of floating point variables, depending upon how the values are set you can have the problem of rounding that causes values that are intended to be found not to be owing to the precision of the two values.
ismembertol is able to help in that case...
@dpb My bad I should have clarified,the HS_elim array and x and y are both dealing with the same set of randomly generated points. First I graph x and y, and in the piece of code before the one above I check to see if any of the points are too close to each other. If so, the pair of points gets recorded in HS_elim and then I want to change both points to NaN values. I really only wanted to change one of them (say the second point in the pair), but I realized its easier to eliminate both from the graph. Thanks for the help.
Seeing that selection code would probably help...sounds like could be simplified if I understand the problem and keeping one of the two shouldn't be much of an issue, either.
Again, actual data and code makes specific answers much easier than trying to infer something from code (particularly code that apparently doesn't do at least quite what is wanted/intended).
Here is the code for the selection. This creates the HS_elim matrix that I want to use to reassign the variables. CTR is the figure handle used to refreshed the plot.
% Circle of influence elimination HS-HS test & isolates hormone seeds that fail condition
for i = 1:n
for j = 1:n
elim_dist2(i,j) = sqrt((x(i)-x(j)).^2 + (y(i)-y(j)).^2);
if (elim_dist2(i,j) ~= 0 && elim_dist2(i,j) < 16)
x_test3 = x(i); y_test3 = y(i);
HS_elim_x = x(j); HS_elim_y = y(j);
testcase3(i,:) = [x_test3, y_test3];
HS_elim(j,:) = [HS_elim_x, HS_elim_y];
end
end
end
LengthofTC4 = length(HS_elim);
for i = 1:n
for j = 1:LengthofTC4
if x(i) == HS_elim(j,1) || y(i) == HS_elim(j,2)
x(i) = NaN;
y(i) = NaN;
refreshdata(CTR);
drawnow
end
end
end
I presume the "~-0" clause is to not eliminate i==j? Or can there be actual identical matches elsewhere?
Either way the first loop
% Circle of influence elimination HS-HS test & isolates hormone seeds that fail condition
threshold=16; % don't bury magic numbers in code...
elim_dist2=diag(inf(n,1))+squareform(pdist([x y])); % distances between points
[HS_elim_x, HS_elim_y]=find(elim_dist2<threshold); % locations under threshold
HS_elim=[HS_elim_x, HS_elim_y];
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); % length --> max(size()) not number rows
for j = 1:LengthofTC4
x(HS_elim_x(j)) = NaN;
y(HS_elim_y(j)) = NaN;
refreshdata(CTR);
drawnow
end
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...
ADDENDUM:
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.
thank you for the help will try this method out and accept if it works. and thanks for the tip about important numbers.
edit: Cannot find the accept answer button but the answer above works.
Well, like so often what started as a comment to try to learn more about the problem morphed into a solution along the way...if it does solve the problem I'll go ahead and make an Answer out of it.

Sign in to comment.

 Accepted Answer

I presume the "~-0" clause is to not eliminate i==j? Or can there be actual identical matches elsewhere?
Either way the first loop
% Circle of influence elimination HS-HS test & isolates hormone seeds that fail condition
threshold=16; % don't bury magic numbers in code...
elim_dist2=diag(inf(n,1))+squareform(pdist([x y])); % distances between points
[HS_elim_x, HS_elim_y]=find(elim_dist2<threshold); % locations under threshold
HS_elim=[HS_elim_x, HS_elim_y];
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); % length --> max(size()) not number rows
for j = 1:LengthofTC4
x(HS_elim_x(j)) = NaN;
y(HS_elim_y(j)) = NaN;
refreshdata(CTR);
drawnow
end
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...
ADDENDUM:
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.

More Answers (0)

Categories

Asked:

on 12 Aug 2019

Answered:

dpb
on 13 Aug 2019

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!