What you're missing is that intersect is a comparison of the elements in the two arrays for exact equality of the members thereof, not a solution to the geometric solution of the intersection of two functions. Even if there had been a point or two that were absolutely identical (to the last bit, remember?) all you would have gotten would have been that one point and it could have been anywhere along the path that just happened to return the same pdf estimate from the two distributions.
What you need to do is to solve for the intersection by writing the difference and find the zero --
On the way, let's make use of some of the more user-friendly Matlab syntax features, ok?
s=load('patients');
pat=struct2table(s);
pat.Gender=categorical(pat.Gender);
[fSmk,xSmk]=ksdensity(pat.Height(pat.Smoker==true));
[fNSmk,xNSmk]=ksdensity(pat.Height(pat.Smoker==false));
Now comes "the trick"...
Build an anonymous function that computes the output difference between the two epdf's as function of the independent variable...use interp1 because the form is numerical, not functional so must interpolate to find points other than those given explicitly.
fnX=@(x) interp1(xSmk,fSmk,x)-interp1(xNSmk,fNSmk,x)
fnX =
@(x)interp1(xSmk,fSmk,x)-interp1(xNSmk,fNSmk,x)
>> fnX(65)
ans =
-0.0346
Now use the function we just defined in fzero to find the intersection point--
>> X0 =fzero(fnX,65)
X0 =
67.5852
>>
Best Answer