I'm trying to match people between two different spreadsheets based on certain variables, how can I match them within +/-2?

1 view (last 30 days)
The purpose is to match people based on their Age, BMI, Sex; but within a deviation of +/-2. I figured out how to create that output but it looks clunky:
for i=1:length(En.SubNumE);
if Pen.AgeP(TargetNum) == En.AgeE(i) || (Pen.AgeP(TargetNum)+1) == En.AgeE(i) || (Pen.AgeP(TargetNum)+2) == En.AgeE(i)
Match(i) = 1; MatchE(j) = En.SubNumE(i); j=j+1;
else
Match(i) = 0;
end
end
is there a less clunky way to do this? Because this is the upper limit, and I realized how clunky it's going to be with additional variables and the lower limit included. I can provide more of the script if this isn't enough context.

Answers (1)

Walter Roberson
Walter Roberson on 21 Dec 2019
ismember(Pen.AgeP(TargetNum), En.AgeE(i)+(-2:2)) %only works for discrete
or
Pen.AgeP(TargetNum) >= En.AgeE(i) - 2 && Pen.AgeP(TargetNum) <= En.AgeE(i) + 2 %also works for continuous

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!