MATLAB Answers

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

조회 수: 4(최근 30일)
Vance Blake
Vance Blake 16 Oct 2019
댓글: Vance Blake 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);
% 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
  댓글 수: 19

댓글을 달려면 로그인하십시오.

채택된 답변

Adam Danz
Adam Danz 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
1 % <---- row 2 of newVN_prime10
matchRow =
36 % <--- row 36 of vein_node_array
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))

추가 답변(0개)

Community Treasure Hunt

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

Start Hunting!

Translated by