How can I calculate the difference between two lines that may consist of NaNs/reoccuring values?
10 次查看(过去 30 天)
显示 更早的评论
I have two lines that I want to compare and calculate the difference between.
The first line consists of points Px, Py and the second line consists of points Cx,Cy (see attached).
I want to compute the distance to the nearest corresponding point along the line. So at x distance, the difference between the two lines is y.
Is there an easy way to do this?
Initially I was using inter1 to interpolate along both lines and compare, however this does not work if the values are not unique.
Cx,Cy also has NaNs as this line involved a detection algorithm and some values were not detected. If I get rid of the NaNs I end up with values that don’t correspond to the correct x or y values.
Another issue is that my lines are not exactly linear and this can cause issues with the interpolation functions. The lines (P and C) do not contain the same number of points.
Is there a better way to calculate the difference between these two lines or is an interpolation method best?
Many thanks in advance, John Hart
0 个评论
回答(1 个)
Duncan Lilley
2018-5-4
Hello John,
There are several things to address here.
First, there are a few ways to handle the NaN values. They can either be interpolated using a function such as fillmissing, or can be removed from the line altogether. Removing them can be done in a similar way as below:
isNaN = isnan(Cx);
Cx(isNaN) = [];
Cy(isNaN) = [];
Once the NaN values have been taken care of, then there is the issue of duplicate x-values. How you handle this depends on how you define the behavior at these points. For example, since "Cy(Cx == 18.5)" is both -163.5 and -168.5, you could just treat the value at this point to be the average of those values, or alternatively the min or max.
Once these values are all sorted out, then you can calculate the difference between the lines by doing something similar to
diffY = Py(Cx == x) - Cy(Cx == x);
for each value, x, in Cx. After taking a look at the values in your Px though, it appears that the x-values do not align between the lines. However, since the P line appears to be linear, you could just calculate its value for a given x. In that case, the above line of code could be replaced with
diffY = x - Cy(Cx == x); % Since line P is linear with slope 1
Since P does not have as large a domain as C, you may need to limit the calculation of the difference to within the smaller domain.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 NaNs 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!