Given a set of linear indexes of a matrix, how can I get only the adjacent indexes from it ?. By adjacent I meant the indexes which are either on the left, right, top, bottom or diagonal position of an index.
For example, given a 4*5 matrix
B = [1 0 0 0 0;
1 1 0 1 1;
0 0 1 0 1;
1 0 1 0 0;]
and the linear indices of B [1, 2, 4, 6, 11, 12, 14, 18, 19] (it corresponds to the indexes of non-zero entreies), how can I select only the one which have at least one neighbour ? In this case, my input is
[1, 2, 4, 6, 11, 12, 14, 18, 19]
I want the output to be
[1, 2, 6, 11, 12, 14, 18, 19]
as except 4, all others have neighbours in the index set.