2
$\begingroup$

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}]; g = AdjacencyGraph[selectionMatrix[0.05], VertexLabels -> MapThread[Rule, {Range [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?

enter image description here

$\endgroup$
4
  • 1
    $\begingroup$ 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]? $\endgroup$ Commented Apr 8, 2019 at 0:41
  • 1
    $\begingroup$ yes. For the two examples in your question, you can use it as selectionMatrix2[mm][.5,1] and selectionMatrix2[mm][-.4,.2] $\endgroup$ Commented Apr 8, 2019 at 1:13
  • 1
    $\begingroup$ @TugrulTemel Have also a look at Clip. $\endgroup$ Commented Apr 8, 2019 at 6:40
  • $\begingroup$ @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. $\endgroup$ Commented Apr 8, 2019 at 11:21

2 Answers 2

1
$\begingroup$

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.

$\endgroup$
4
  • $\begingroup$ I installed IGraph but BoolEval does not work properly. I receive the following warning: Get::noopen: Cannot open BoolEval`while 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? $\endgroup$ Commented Apr 8, 2019 at 10:38
  • 1
    $\begingroup$ @TugrulTemel These are two independent packages. It looks like you did not install BoolEval. $\endgroup$ Commented Apr 8, 2019 at 10:40
  • 1
    $\begingroup$ @TugrulTemel In recent versons of Mathematica, PacletInstall["https://github.com/szhorvat/BoolEval/releases/download/v1.0.0/BoolEval-1.0.0.paclet"] $\endgroup$ Commented Apr 8, 2019 at 10:40
  • $\begingroup$ I found the way to enter a matrix. Thank you very much for the answer. $\endgroup$ Commented Apr 8, 2019 at 12:02
1
$\begingroup$

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

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

enter image description here

MatrixForm[AdjacencyMatrix[g]] 

enter image description here

$\endgroup$

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.