Filter löschen
Filter löschen

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

1 Ansicht (letzte 30 Tage)
JamJan
JamJan am 24 Sep. 2019
Kommentiert: David Hill am 24 Sep. 2019
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 Kommentar
David Hill
David Hill am 24 Sep. 2019
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

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Andrei Bobrov
Andrei Bobrov am 24 Sep. 2019
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)];

Kategorien

Mehr zu View and Analyze Simulation Results finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by