Azzera filtri
Azzera filtri

How can i find the elements of an array with in tolerance and map them to a unique number if those number are within that tolerance?

5 visualizzazioni (ultimi 30 giorni)
Hi every one
i have an array A=[0.5 1 1.75 2 5 5.25 5.75 6 6.25 9 9.25 9.5 10.25], now i want to find elements of this array that are closer than (A(i)-A(j)<2) to each other and map them to an array such M=[1 1 1 1 2 2 2 2 2 3 3 3 3] in which for example the elements 0.5, 1, 1.75 and 2 are all maped to 1 in the array M. can enyone please help me with this?
thank you

Risposta accettata

Guillaume
Guillaume il 18 Gen 2019
Modificato: Guillaume il 18 Gen 2019
This requires the image processing toolbox (for bwlabel). It also requires that A is sorted (which appears to be the case in your example). Finally, note that the following will also group together [1 2 3 4] since the difference between consecutive numbers is < 2 even the the difference between the extreme is > 2. If you don't want that then you need to explain how the numbers should be grouped in that case.
A=[0.5 1 1.75 2 5 5.25 5.75 6 6.25 9 9.25 9.5 10.25];
assert(issorted(A), 'A must be sorted');
result = diag(bwlabel(abs(A - A.') <= 2, 4)).'
edit: Actually, it can be done easily without the image processing toolbox. The same conditions still apply:
A=[0.5 1 1.75 2 5 5.25 5.75 6 6.25 9 9.25 9.5 10.25];
assert(issorted(A), 'A must be sorted');
result = cumsum([1, diff(A) > 2])

Più risposte (1)

madhan ravi
madhan ravi il 18 Gen 2019
doc uniquetol

Community Treasure Hunt

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

Start Hunting!

Translated by