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

5 views (last 30 days)
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.
rEMGb=round(emg_time(:),3);
rEMGa=round(eventTime,3);
[~,emgCueIDX]=ismember(rEMGa,rEMGb);
  1 Comment
Jeffrey Clark
Jeffrey Clark on 20 Jun 2022
@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.

Sign in to comment.

Answers (1)

Star Strider
Star Strider on 20 Jun 2022
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
end
figure
plot(x, y)
hold on
plot(xv, yv, 'sr')
hold off
xline(xl)
xlabel('X')
ylabel('Y')
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.
.

Categories

Find more on Resizing and Reshaping Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!