# Generating a constrained directed graph from an adjacency matrix

I have written the following Code

Clear[n, d, A, mm, G];
SeedRandom[14];
n = 10;
A = Array[RandomReal[{0, 0.5}] &, {n, n}];
mm = SetPrecision[Inverse[IdentityMatrix[n] - A], 2];

selectionMatrix[θ_] := ReplacePart[mm, {i_, i_} -> 0] /.
{x_ /; x > θ -> 1, x_ /; x < θ -> 0} // Transpose;

v = Table[Subscript[s, i], {i, n}];
[Length[v]], v}], ImageSize -> 150, VertexSize -> .9, ImagePadding -> 20];

{selectionMatrix[0.05] // MatrixForm, g}


selectionMatrix works just fine for a single parameter theta defining the threshold for creating an adjacency matrix represented as a directed graph. The parameter theta defines a binary selection, i.e., selecting those elements of a matrix that are below or above the theta level.

I want to modify the selectionMatrix function in such a way to create an adjacency matrix for a range of theta such as 0.5<theta<1 or -0.4<theta<0.2 etc. How can I revise selectionMatrix to accomplish this task?

• does this work: ClearAll[selectionMatrix2]; selectionMatrix2[m_][\[Theta]1_, \[Theta]2_] := Transpose[(1 - UnitStep[\[Theta]1 - m]) (1 - UnitStep[m - \[Theta]2])] - IdentityMatrix[Length@m]?
– kglr
Apr 8, 2019 at 0:41
• yes. For the two examples in your question, you can use it as selectionMatrix2[mm][.5,1] and selectionMatrix2[mm][-.4,.2]
– kglr
Apr 8, 2019 at 1:13
• @TugrulTemel Have also a look at Clip. Apr 8, 2019 at 6:40
• @kglr: Unfortunately, it does not give the correct result. Given a matrix mm = {{1, 2, 3, 4}, {0, -1, -2, -3}, {0, 1, -3, 3}, {3, 4, 5, -5}}, selectionMatrix2[mm][-2, 2] (elements within the range (-2,2) should be all 1, otherwise 0, including diagonals) yields {{0, 1, 1, 0}, {0, 0, 1, 0}, {0, 0, -1, 0}, {0, 0, 0, -1}}, which is wrong. Apr 8, 2019 at 11:21

Check out the BoolEval package:

<< BoolEval

BoolEval[0.5 < Range[0, 1, 0.1] < 1]
(* {0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0} *)

BoolEval[0.5 < Range[0, 1, 0.1] <= 1]
(* {0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1} *)


You can use it on the mm matrix.

Also check out IGZeroDiagonal from IGraph/M for efficiently and reliably zeroing out a diagonal of a matrix.

• I installed IGraph but BoolEval does not work properly. I receive the following warning: Get::noopen: Cannot open BoolEvalwhile I have the currently installed versions of IGraph/M are: {Paclet[IGraphM,0.3.108,<>],Paclet[IGraphM,0.3.103,<>]}. What is the problem here? Apr 8, 2019 at 10:38
• @TugrulTemel These are two independent packages. It looks like you did not install BoolEval. Apr 8, 2019 at 10:40
• @TugrulTemel In recent versons of Mathematica, PacletInstall["https://github.com/szhorvat/BoolEval/releases/download/v1.0.0/BoolEval-1.0.0.paclet"] Apr 8, 2019 at 10:40
• I found the way to enter a matrix. Thank you very much for the answer. Apr 8, 2019 at 12:02

It is just the probabilities that matter, not the thresholds:

theta2 = .6;
theta1 = .4;
g = RandomGraph[
BernoulliGraphDistribution[10, Abs[theta2 - theta1]]]


MatrixForm[AdjacencyMatrix[g]]