Find index of element closest to other index

2 Ansichten (letzte 30 Tage)
Mark Laterius
Mark Laterius am 15 Okt. 2020
Bearbeitet: Stephen23 am 15 Okt. 2020
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.

Akzeptierte Antwort

Stephen23
Stephen23 am 15 Okt. 2020
Bearbeitet: Stephen23 am 15 Okt. 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.

Weitere Antworten (0)

Kategorien

Mehr zu Programming finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by