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?
9 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Joseph
el 18 de En. de 2019
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
0 comentarios
Respuesta aceptada
Guillaume
el 18 de En. de 2019
Editada: Guillaume
el 18 de En. de 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])
0 comentarios
Más respuestas (1)
Ver también
Categorías
Más información sobre Shifting and Sorting Matrices en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!