How to compare two vectors on distance and isolate consecutive segments?
2 views (last 30 days)
Show older comments
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 Comment
David Hill
on 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
Answers (1)
Andrei Bobrov
on 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)];
See Also
Categories
Find more on Statics and Dynamics in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!