Keep pairs of corresponding elements in 2 vectors based on conditions

조회 수: 2 (최근 30일)
fadams18
fadams18 2023년 3월 14일
편집: Dyuman Joshi 2023년 3월 16일
I have the problem here: Using this image below: I want to check for several conditions
the orange and green marks are annotations on a signal. If we consider vectors orange, and green which contain the points needed to make the annotations.
The point is to have perfect pairs and ignore all others. i.e. orange(i) and green(i) is a pair if orange(i)<green(i)
so all values in vector orange are lower than their corresponding elements in vector green.
I want to check the following conditions.
I started but some conditions dont work, thats why i need help.
Condition 1: orange must always begin, so if a green begins I delete that green point. [done]
condition 2: if 2 or more greens come before an orange, delete all unecessary green to keep a pair.
condition 3: similarly if several orange exist delete them to keep a pair.
so in the end I want to have a perfect system of pairs, where orange starts a pair. like this:
orange=[212929;213223;213482;213740;214013;214790];
green= [212765;213301;213822;214090;214240;214426;214941];
if green(1)<orange(1)
green=green(2:end);
end
lenss =min(length(orange),length(green));
i = 1;
while i <= lenss
if abs(green(i)-orange(i)) <0
green(i) = [];
i=1;
continue;
else
i=1+1;
continue;
end
end
  댓글 수: 1
Jan
Jan 2023년 3월 14일
편집: Jan 2023년 3월 14일
abs(green(i)-orange(i)) <0
The output of abs() is >= 0 for all inputs.
The two continue statements have no effect.

댓글을 달려면 로그인하십시오.

채택된 답변

Jan
Jan 2023년 3월 14일
편집: Jan 2023년 3월 14일
orange = [212929;213223;213482;213740;214013;214790];
green = [212765;213301;213822;214090;214240;214426;214941];
[x, s] = sort([orange.', green.']);
index = strfind(s <= numel(orange), [true, false]);
ok_orange = x(index)
ok_orange = 1×4
213223 213740 214013 214790
ok_green = x(index + 1)
ok_green = 1×4
213301 213822 214090 214941
The idea: sort all elements. If their sorting index is greater than the number of orange elements, they are green. So s <= numel(orange) is a logical vector, which is TRUE for orange and FALSE for green. Now find the neighboring [true, false] pairs.
Kind regards to @Bruno Luong, who told me this method over 20 years ago. :-)
  댓글 수: 5
Jan
Jan 2023년 3월 16일
@Dyuman Joshi: Since 1999. I used some other languages in the 15 years before.
Dyuman Joshi
Dyuman Joshi 2023년 3월 16일
편집: Dyuman Joshi 2023년 3월 16일
Wow, just shy of my time on this planet!
What prompted you to try out MATLAB at that time and to stick with it for so long and continue using it? (If you don't mind me asking)
(I feel like this would be an interesting conversation to have with the MATLAB Answers community!)

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Signal Processing Toolbox에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by