Find index of element closest to other index

1 view (last 30 days)
I have two binary vectors, b1 and b2, with values of either 0 or 1. b1 has always fewer true values or ones than b2. The true values rarely overlap, i.e. have the same index.
What I want to do is to use the index of when b1 is 1 and set the closest true value or 1 in b2 to false or 0.
Basically I want to remove duplicates from b2, but if they don't overlap I want to remove closest true value.

Accepted Answer

Stephen23
Stephen23 on 15 Oct 2020
Edited: Stephen23 on 15 Oct 2020
>> B1 = [0,0,1,0,0,0,1,0,0,0,0,1,0,0,0,1]
B1 =
0 0 1 0 0 0 1 0 0 0 0 1 0 0 0 1
>> B2 = [1,1,0,1,1,0,0,1,1,1,0,0,0,0,1,1]
B2 =
1 1 0 1 1 0 0 1 1 1 0 0 0 0 1 1
>> X1 = find(B1);
>> X2 = find(B2);
>> X3 = interp1(X2,X2,X1,'nearest');
>> B2(X3) = 0
B2 =
1 1 0 0 1 0 0 0 1 0 0 0 0 0 1 0
Note that the definition of "nearest" is open to interpretation: what happens if there are two 1's in B2 exactly the same distance from a 1 in B1? (the first 1 in B1 is an example of this)
You might need to check the edge-cases as well... probably some fine-tuning is required.

More Answers (0)

Categories

Find more on Programming in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!