MATLAB: How to find intersection between two outputs

intersect

Dear Coder,
I have to output generated from KSDENSITY, namely f_smoke and f_Nonsmoke. The idea was to find intersection between the f_smoke and f_Nonsmoke. To achieve this, I used the function INTERSECT. However, MATLAB return me 1×0 empty double row vector instead.
C = intersect(f_smoke,f_Nonsmoke);.
I expect, the intersection should occur as depicted in the figure
Any idea what I have been missing?. The full code is as below. The patients.mat is an in-build matlab mat file (Version 2017).
load patients
[tf,Ioc_Alert_LessThree] = find (ismember(Smoker, 1));
for i = 1: length (tf)
M (i,1) = Height (tf(i));
end
[tgf,Ioc_Alert_two] = find (ismember(Smoker, 0));
for i = 1: length (Ioc_Alert_two)
M (i,2) = Height (tgf(i));
end
M(M == 0) = NaN;
[f_smoke,xi_smoke] Th= ksdensity(M (1:end,2));
[f_Nonsmoke,xi_Nonsmoke] = ksdensity(M (1:34,1));
plot (xi_smoke, f_smoke);
hold on
plot (xi_Nonsmoke, f_Nonsmoke);
C = intersect(f_smoke,f_Nonsmoke);
Thanks for the time and idea!

Best Answer

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'); % load the data into structure in preparation for...
pat=struct2table(s); % creating a table structure from it...
pat.Gender=categorical(pat.Gender); % just a sample cleanup to turn from cellstr to categorical
[fSmk,xSmk]=ksdensity(pat.Height(pat.Smoker==true)); % fit the empircal density to smokers
[fNSmk,xNSmk]=ksdensity(pat.Height(pat.Smoker==false)); % and non in turn...
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) % just a sample use of the function to check it works...skip this, just demo
ans =
-0.0346
Now use the function we just defined in fzero to find the intersection point--
>> X0 =fzero(fnX,65) % solve for the intersection; use 65 as initial guess
X0 =
67.5852
>>