Is it possible to find the index of an array based on the Xline?

1 ビュー (過去 30 日間)
Dc215905 2022 年 6 月 17 日
回答済み: Star Strider 2022 年 6 月 20 日
I have a dataset where I need to identify specific timepoints. The figure below is an example plot where I need to know what Index of the yellow line cross with the vertical lines?
I'm having trouble using ismember to find the exact match between two arrays so I was wondering if I can use Xline to help? Any alternative solution would be very helpful. I've tried using the following code to deternime the IDX where eventTime (vertical line of the image meets the yellow line in the image, but it doesn't consistently work. It doesn't matter to what degree I change the rounding.
  1 件のコメント
Jeffrey Clark
Jeffrey Clark 2022 年 6 月 20 日
@Dc215905, you should probably use ismembertol if the points that make up the lines are spaced such that they should be within tol of each other. Otherwise if you really want the value (not index) of the yellow line crossing the blue, you could spline the yellow line values against their times and use ppval to get the approximate value at the crossing times.


回答 (1 件)

Star Strider
Star Strider 2022 年 6 月 20 日
I’n not exactly certain what you want, however this is relatively straightforward using the find function, and getting the intersection points of the curve with the xline values is straightforward with interp1
x = linspace(0, 10);
y = sin(2*pi*x*3/max(x));
xl = sort(10*rand(1,5)); % Random 'xline' Values
for k = 1:numel(xl)
xi = find(x <= xl(k), 1, 'last'); % Nearest Index To 'xline' Value
idxrng = max(1,xi-1) : min(numel(x),xi+1); % Index Range For Interpolation
yv(k) = interp1(x(idxrng), y(idxrng), xl(k)); % 'y' Coordinate Of 'xline' Intersection
xv(k) = interp1(y(idxrng), x(idxrng), yv(k)); % 'x' Coordinate Of 'xline' Intersection
plot(x, y)
hold on
plot(xv, yv, 'sr')
hold off
legend('Data','Intersectionos','Xlines', 'Location','best')
This works well enough with my sample data, however I can’t determine if it will be equally robust with your data.

Community Treasure Hunt

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

Start Hunting!

Translated by