4 views (last 30 days)

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

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))

Sign in to comment.

Sign in to answer this question.

Opportunities for recent engineering grads.

Apply Today
## 19 Comments

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/485730-define-an-if-statement-based-on-on-finding-duplicate-xy-pair-values-between-arrays#comment_757039

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/485730-define-an-if-statement-based-on-on-finding-duplicate-xy-pair-values-between-arrays#comment_757039

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/485730-define-an-if-statement-based-on-on-finding-duplicate-xy-pair-values-between-arrays#comment_757074

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/485730-define-an-if-statement-based-on-on-finding-duplicate-xy-pair-values-between-arrays#comment_757074

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/485730-define-an-if-statement-based-on-on-finding-duplicate-xy-pair-values-between-arrays#comment_757078

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/485730-define-an-if-statement-based-on-on-finding-duplicate-xy-pair-values-between-arrays#comment_757078

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/485730-define-an-if-statement-based-on-on-finding-duplicate-xy-pair-values-between-arrays#comment_757081

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/485730-define-an-if-statement-based-on-on-finding-duplicate-xy-pair-values-between-arrays#comment_757081

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/485730-define-an-if-statement-based-on-on-finding-duplicate-xy-pair-values-between-arrays#comment_757085

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/485730-define-an-if-statement-based-on-on-finding-duplicate-xy-pair-values-between-arrays#comment_757085

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/485730-define-an-if-statement-based-on-on-finding-duplicate-xy-pair-values-between-arrays#comment_757086

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/485730-define-an-if-statement-based-on-on-finding-duplicate-xy-pair-values-between-arrays#comment_757086

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/485730-define-an-if-statement-based-on-on-finding-duplicate-xy-pair-values-between-arrays#comment_757098

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/485730-define-an-if-statement-based-on-on-finding-duplicate-xy-pair-values-between-arrays#comment_757098

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/485730-define-an-if-statement-based-on-on-finding-duplicate-xy-pair-values-between-arrays#comment_757109

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/485730-define-an-if-statement-based-on-on-finding-duplicate-xy-pair-values-between-arrays#comment_757109

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/485730-define-an-if-statement-based-on-on-finding-duplicate-xy-pair-values-between-arrays#comment_757116

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/485730-define-an-if-statement-based-on-on-finding-duplicate-xy-pair-values-between-arrays#comment_757116

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/485730-define-an-if-statement-based-on-on-finding-duplicate-xy-pair-values-between-arrays#comment_757127

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/485730-define-an-if-statement-based-on-on-finding-duplicate-xy-pair-values-between-arrays#comment_757127

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/485730-define-an-if-statement-based-on-on-finding-duplicate-xy-pair-values-between-arrays#comment_757129

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/485730-define-an-if-statement-based-on-on-finding-duplicate-xy-pair-values-between-arrays#comment_757129

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/485730-define-an-if-statement-based-on-on-finding-duplicate-xy-pair-values-between-arrays#comment_757130

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/485730-define-an-if-statement-based-on-on-finding-duplicate-xy-pair-values-between-arrays#comment_757130

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/485730-define-an-if-statement-based-on-on-finding-duplicate-xy-pair-values-between-arrays#comment_757132

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/485730-define-an-if-statement-based-on-on-finding-duplicate-xy-pair-values-between-arrays#comment_757132

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/485730-define-an-if-statement-based-on-on-finding-duplicate-xy-pair-values-between-arrays#comment_757133

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/485730-define-an-if-statement-based-on-on-finding-duplicate-xy-pair-values-between-arrays#comment_757133

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/485730-define-an-if-statement-based-on-on-finding-duplicate-xy-pair-values-between-arrays#comment_757227

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/485730-define-an-if-statement-based-on-on-finding-duplicate-xy-pair-values-between-arrays#comment_757227

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/485730-define-an-if-statement-based-on-on-finding-duplicate-xy-pair-values-between-arrays#comment_757300

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/485730-define-an-if-statement-based-on-on-finding-duplicate-xy-pair-values-between-arrays#comment_757300

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/485730-define-an-if-statement-based-on-on-finding-duplicate-xy-pair-values-between-arrays#comment_757428

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/485730-define-an-if-statement-based-on-on-finding-duplicate-xy-pair-values-between-arrays#comment_757428

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/485730-define-an-if-statement-based-on-on-finding-duplicate-xy-pair-values-between-arrays#comment_757432

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/485730-define-an-if-statement-based-on-on-finding-duplicate-xy-pair-values-between-arrays#comment_757432

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/485730-define-an-if-statement-based-on-on-finding-duplicate-xy-pair-values-between-arrays#comment_757494

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/485730-define-an-if-statement-based-on-on-finding-duplicate-xy-pair-values-between-arrays#comment_757494

Sign in to comment.