How to compare two vectors on distance and isolate consecutive segments?
2 次查看(过去 30 天)
显示 更早的评论
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
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
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 Center 和 File Exchange 中查找有关 Statics and Dynamics 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!