フィルターのクリア

Interp1 - The grid vectors must contain unique points

5 ビュー (過去 30 日間)
D F
D F 2017 年 11 月 6 日
コメント済み: Star Strider 2017 年 11 月 6 日
I have a text file that has been plotted and I need to find the x value from a known y value. However there isn't a data point at this point so I have interpolated. This has worked for some txt files but not others.
ymax = max(y);
xmax = find(y == ymax);
xmax = x(xmax);
yhalfmax = max(y)/2;
xhalfmax = interp1(y, x, yhalfmax, 'spline'); %interpolate to generate a point at the yhalfmax point
Error: Error using gridded Interpolant. The grid vectors must contain unique points.
I have tried using 'unique' but this has changed the plotting so can't be used.
  2 件のコメント
Matt J
Matt J 2017 年 11 月 6 日
I have tried using 'unique' but this has changed the plotting so can't be used.
How can it "change the plotting", if all you've done is throw away duplicate points?
D F
D F 2017 年 11 月 6 日
When the following is used to replace the interp1 line above, the curve changes shape. I'm unsure why this would change the plot curve unless it is deleting usefull data points
[x, index] = unique(y); xhalfmax = interp1(y(index), x,yhalfmax, 'linear');

サインインしてコメントする。

回答 (1 件)

Star Strider
Star Strider 2017 年 11 月 6 日
My usual approach to the problem of non-unique independent variable values for interp1 is to add a very small, increasing value to each element.
Example
XData = sort(randi(9, 1, 10)) % Create Data
XDataUnique = XData + linspace(0, 1, length(XData))*1E-3 % Add Increments To Each Element
I used ‘1E-3’ here to illustrate the idea. In practice, I use a much smaller multiplier, ‘1E-10’ or so.
  4 件のコメント
D F
D F 2017 年 11 月 6 日
Aaah okay that makes sense! I have run the code and it is still returning the same error.
I then exchanged the x in the interp1 line of my code for XDataUnique however the index exceeded the matrix dimension, any thoughts?
Star Strider
Star Strider 2017 年 11 月 6 日
Looking at your code, it seems that you need to use it to create your ‘y’ data to do your interpolation, not your ‘x’ data.
Try this:
y = sort(randi(9, 1, 10)) % Create Data
YDataUnique = y + linspace(0, 1, length(y))*1E-3 % Add Increments To Each Element
I honestly have no idea what is causing the index error. The vector size should not change with my code.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeInterpolation についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by