Is it possible to find the index of an array based on the Xline?
조회 수: 4 (최근 30일)
이전 댓글 표시
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
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
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.
.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!