Find the elements of array with the difference between one of the remaining elements less than specified

1 visualización (últimos 30 días)
Dear members of Matlab community,
I have this array (as an example):
A=[1 18 25; 26 30 11; 5 31 20]
A = 3×3
1 18 25 26 30 11 5 31 20
How can I find the indices of the elements with the difference between one of the remaining elements less than 2?
In this case, it must be elements A(1,3), A(2,1), A(2,2), A(3,2). If we compare one of these elements with one of the remaining elements, the difference between them is less than 2 (25 and 26, 30 and 31, respectively).
Thank you for your help.

Respuesta aceptada

Matt J
Matt J el 25 de Mzo. de 2023
Editada: Matt J el 25 de Mzo. de 2023
A=[1 18 25; 26 30 11; 5 31 20];
thresh=2;
[m,n]=size(A); p=m*n;
D=abs( A(:)-A(:)' ); D(1:p+1:end)=inf;
[I,J]=find(reshape( any(D<thresh,2) , [m,n]));
result=[I,J]
result = 4×2
2 1 2 2 3 2 1 3

Más respuestas (0)

Categorías

Más información sobre Matrix Indexing 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!

Translated by