필터 지우기
필터 지우기

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

조회 수: 5 (최근 30일)
Dc215905
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.
rEMGb=round(emg_time(:),3);
rEMGa=round(eventTime,3);
[~,emgCueIDX]=ismember(rEMGa,rEMGb);
  댓글 수: 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
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.
.

카테고리

Help CenterFile Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by