Constructing adjacency matrix based on a given range of numbers

With vectorized ops

Clip[
  mat, 
  {-2, 1},
  {0, 0}
  ] // Unitize

{
  {1, 0, 1, 0, 0}, 
  {1, 1, 0, 1, 0}, 
  {0, 1, 0, 0, 0}, 
  {0, 1, 0, 0, 1}, 
  {0, 0, 1, 0, 0}
 }

or as a function

adjMat[mat_, lb_, ub_] :=
 Clip[
   mat, 
   {lb, ub},
   {0, 0}
   ] // Unitize

ClearAll[adjMat2]

adjMat2[matrix_, lB_, uB_] := Map[Boole[lB <= # <= uB] &, matrix, {-1}]

adjMat2[mat, -2, 1] // MatrixForm

enter image description here

ClearAll[adjMat3]
adjMat3[matrix_, lB_, uB_] := UnitStep[matrix - lB] UnitStep[uB - matrix]

adjMat3[mat, -2, 1] // MatrixForm

enter image description here

And, for fun,

PrecedesTilde = Boole @* Map[Thread] @* Thread @* LessEqual;

-2 ≾ mat ≾ 1 
{{1, 0, 1, 0, 0}, {1, 1, 0, 1, 0}, {0, 1, 0, 0, 0}, 
 {0, 1, 0, 0, 1}, {0, 0, 1, 0, 0}}
MatrixForm @ %

enter image description here


I would use UnitBox[] for this:

adjMat[mat_, lb_, ub_] := UnitBox[(2 mat - (lb + ub))/(2 (ub - lb))]

Using OP's example:

BlockRandom[SeedRandom[3];
            With[{n = 5}, mat = RandomReal[{-4, 4}, {n, n}]];
            adjMat[mat, -2, 1]]
   {{1, 0, 1, 0, 0}, {1, 1, 0, 1, 0}, {0, 1, 0, 0, 0}, {0, 1, 0, 0, 1}, {0, 0, 1, 0, 0}}

In a lot of cases, one might want a SparseArray[] instead; here's how to get one:

BlockRandom[SeedRandom[3];
            With[{n = 5}, mat = RandomReal[{-4, 4}, {n, n}]];
            SparseArray[Position[mat, _?(Between[{-2, 1}]), {2}] -> 1, 
                        Dimensions[mat]]]