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?

조회 수: 3 (최근 30일)
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

채택된 답변

Guillaume
Guillaume 2019년 1월 18일
편집: Guillaume 2019년 1월 18일
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])

추가 답변 (1개)

madhan ravi
madhan ravi 2019년 1월 18일
doc uniquetol

카테고리

Help CenterFile Exchange에서 3-D Volumetric Image Processing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by