How to compare two vectors on distance and isolate consecutive segments?

조회 수: 1 (최근 30일)
JamJan
JamJan 2019년 9월 24일
댓글: David Hill 2019년 9월 24일
Hi,
I want to compare/melt two vectors that represent two data strains that are closely connected to each other:
A = [134 163 185 207 229 251 273 295 319 1277 1364 1760 1793 1823 1851 1880 1909 1939 1969 2003];
B = [144 172 194 216 238 260 282 305 327 1805 1835 1863 1892 1921 1950 1983];
What I want is that it creates a single vector that contains A and B together, so that it alternates. Here, the condition is that it has to alternate from A to B / B to A depending on the smaller number and that the difference between the alternating numbers can't be bigger 40. Also I want to know which numbers don't fit. So the output would be:
Output = [134 144 163 172 185 194 207 216 229 238 251 260 273 282 295 305 319 327 1793 1805 1823 1835 1851 1863 1880 1892 1909 1921 1939 1950 1969 1983 2003]
LeftOut = [1277 1364 1760]
Does anyone has a good suggestion how to solve this?
  댓글 수: 1
David Hill
David Hill 2019년 9월 24일
Output=[];
LeftOut=[];
for i=1:length(A)
if abs(A(i)-B(i))<=40
Output=[Output,A(i),B(i)];
else
LeftOut=[LeftOut,A(i)];
end
end

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

답변 (1개)

Andrei Bobrov
Andrei Bobrov 2019년 9월 24일
d = abs(A(:) - B(:)') <= 40;
loA = any(d,2);
loB = any(d,1);
output = zeros(nnz([loA(:);loB(:)]),1);
output(1:2:end) = A(loA);
output(2:2:end) = B(loB);
LeftOut = [A(~loA),B(~loB)];

카테고리

Help CenterFile Exchange에서 View and Analyze Simulation Results에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by