# Define an if statement based on on finding duplicate xy pair values between arrays.

4 views (last 30 days)
Vance Blake on 16 Oct 2019
Commented: Vance Blake on 17 Oct 2019
Hello, I have 2 arrays of different sizes which I am trying to compare for duplicates using an if statement. The condition to activate this section of the code should be if there are any duplicate xy sets in between the two arrays then do these commands. Any ideas or tips for improvement will be greatly appreciated thank you. Also I know that I switch variable names a lot and its confusing.
% test for find repeat combined vein nodes/vectors
if newVN_prime10 == vein_node_array % if any values in the 2 arrays are equal this code should activate the loop
VN_VN_threshold = ((1/2)*radii_vein); % if distance between VNs is greater than this value then point should be retained
SizeNewVN_prime10 = size(newVN_prime10, 1);
newVN_prime10x = newVN_prime10(:,1);
newVN_prime10y = newVN_prime10(:,2);
reconfig_dist10 = nan(numel(VNA_x)); % preallocation step
for j = 1:SizeofVNA
for i = 1:SizeNewVN_prime10
reconfig_dist10(j,i) = sqrt((newVN_prime10x(i)-VNA_x(j)).^2 + (newVN_prime10y(i)-VNA_y(j)).^2);
end
end
% find the points that are greater than VN_VN_threshold
i2keepVN_VN = find(min(reconfig_dist10)> VN_VN_threshold); % keeps the VN points that pass the test
inot2keepVN_VN = find(min(reconfig_dist10)< VN_VN_threshold); % keeps the VN points that fail the test
% store points greater than VN_VN_threshold
x_newVN_retained = newVN_prime10x(i2keepVN_VN);
y_newVN_retained = newVN_prime10y(i2keepVN_VN);
% store points less than VN_VN_threshold
x_notkept = newVN_prime10x;
y_notkept = newVN_prime10y;
x_notkept(i2keepVN_VN) = [];
y_notkept(i2keepVN_VN) = [];
newVN_retained = [x_newVN_retained, y_newVN_retained]; % store new vein nodes the pass test
VN_VectorGroupNum = inot2keepVN_VN; % VN group # needs to be kept for linking back to proper VNA point
VN_reconfig = VN_HSvectors10{VN_VectorGroupNum}; % grabs the vector group that makes up repeated combined vector
VN_HScomb10Beta = 8*(VN_reconfig./vecnorm(VN_reconfig,2,2)); % turns vector group into scaled unit vectors
SizeVN_HScomb10Beta = size(VN_HScomb10Beta,1);
VNconfig_beta = VN_HScomb10Beta(:,:) + vein_node_array(GroupList10(VN_VectorGroupNum),:); % adds reconfigured vectors to appropriate vein node chain
end % end of if statement

Vance Blake on 17 Oct 2019
Alright I see what you guys are saying, it is important to account for roundoff error. Now I was thinking about the second problem and in order for us to be discussing efficiently I have to provide all the information needed for you to see exactly what Im seeing and therefore understand what im asking more clearly. So Im gonna take a moment to cobble all the bits of code, arrays, and variables needed to run this section properly Im gonna post it after this comment. Also I think it's better to put this problem under a separate question, so Im gonna edit this question title to just the if statement and make another question specific to the second problem. So if either of you wants to make the ismembertol solution into answer, I'll accept it so we can close this thread and not confuse anyone in the future too much. Again, thanks for all your help.
Adam Danz on 17 Oct 2019
Ha! Good idea.
Vance Blake on 17 Oct 2019

Adam Danz on 17 Oct 2019
(continuing from the comment section under the question)
I recommend you use ismembertol() which allows you to set a small tolerance level so that roundoff error does not prevent finding matches.
[isMatch, matchRow] = ismembertol(newVN_prime10, vein_node_array,0.000001, 'ByRows',true)
isMatch =
3×1 logical array
0
1 % <---- row 2 of newVN_prime10
0
matchRow =
0
36 % <--- row 36 of vein_node_array
0
NOTE: the matchRow will return the first row in vein_node_array that provides the match. If you need a list of all rows that match, we'll need to make a modification (see comments under the question).
To return a true/false indicating whether there was a match or not,
TF = any(ismembertol(newVN_prime10, vein_node_array,0.000001, 'ByRows',true))