How to find intersection between two outputs?
Mostrar comentarios más antiguos
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!
Respuesta aceptada
Más respuestas (2)
Geoff Hayes
el 23 de Jul. de 2017
balandong - remember, the data that you are plotting are doubles and you are drawing a curve where every two consecutive points is "connected" to each other. So when the two curves are drawn, their intersection (where the two curves cross) may not be represented by data from your sets...and so intersect is not guaranteed to be non-empty. Consider the following
plot (xi_smoke, f_smoke, 'r*');
hold on
plot (xi_Nonsmoke, f_Nonsmoke,'g*');
Each point from the first set is represented by a red star, and each from the second set by a green star. Now when you run the code, you can see while there are points close to one another, there doesn't appear to be any that actually intersect.
I suspect that you will need to loop through your data and try to find the two (or more points) that are "close" to one another using a Euclidean or some other distance metric. You may be able to discover the "intersection" from those points.
1 comentario
balandong
el 23 de Jul. de 2017
Image Analyst
el 23 de Jul. de 2017
You can do it approximately and numerically by using comparison to find the index where the one curve goes above or below the other curve. Let's say non-smokers are red (to the right because they live longer) and smokers are above and to the left in blue. Find the index where the blue dips below the red:
index = find(f_smoke < f_Nonsmoke, 1, 'first');
% Get values of the two curves at that index.
smokeCrossoverValue = f_smoke(index);
nonsmokeCrossoverValue = f_Nonsmoke(index);
If the x value is not the index, then you can get the x value (Age, I presume) by indexing it:
ageAtCrossover = x(index);
If you want you could home in on it a little more by doing bilinear interpolation between index and index-1.
4 comentarios
balandong
el 24 de Jul. de 2017
The problem is again that the logical expression f_smoke<f_Nonsmoke is a 1:1 comparison of the elements in the arrays which are not at all at the same locations of x since the two are computed independently. To do this you'd have either
- Align the two distributions at comparable x values before the comparison, or
- Compute the two density functional values at a set of matching x values before the comparison.
Having plotted the two on the same axis is misleading in that one can easily assume the x- values are the same but they have no relationship to each other the way ksdensity works with default outputs.
Image Analyst
el 24 de Jul. de 2017
Right. I agree it turned out to be trickier than I thought because they don't have the same x-values.
balandong
el 25 de Jul. de 2017
Categorías
Más información sobre Creating and Concatenating Matrices en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!