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

1 次查看(过去 30 天)
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.

回答(1 个)

Walter Roberson
Walter Roberson 2019-12-21
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

类别

Help CenterFile Exchange 中查找有关 Get Started with MATLAB 的更多信息

产品


版本

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by