This code
performs as expected, but it is too scattered and I like to create, if possible, a Mathematica
function sayanTest[matrix_, excLst_]:=
to do the following by combining Parts 1-4
:
ClearAll[dim, mat1, excLst, mat2, MMmat1, mat211, mat212, mat221,
mat222, mm0111, mm1011, mm1101, mm1110, nodes, wag];
multiplierB[M_?MatrixQ,
sec_Integer] := ((#/Tr[#] & /@ N[M\[Transpose]])\[Transpose] //
Take[#, sec, sec] & // Inverse[IdentityMatrix[sec] - #] &);
select[matrix_, lB_, uB_] :=
matrix*Map[Boole[lB <= # <= uB] &, matrix, {-1}];
(*Part 1*)
SeedRandom[14];
dim = 15;
mat1 = RandomInteger[{1, 10}, {dim, dim}];
excLst = {5, 3, 1, 2, 4}; (*a specific order of rows and columns*)
MMmat1 = multiplierB[mat1, Length[excLst]];
mat2 = MMmat1[[excLst, excLst]]; (*ordered matrix*)
(*Part 2*)
(*create sub-matrices as block matrices of {5,3} and {1,2,4} *)
mat211 = Take[mat2, 2, 2];
mat212 = Take[mat2, 2, -3];
mat221 = Take[mat2, -3, 2];
mat222 = Take[mat2, -3, -3];
(*substitute zeros in a sub-matrix*)
mm0111 = ArrayFlatten[{{0, mat212}, {mat221, mat222}}];
mm1011 = ArrayFlatten[{{mat211, 0}, {mat221, mat222}}];
mm1101 = ArrayFlatten[{{mat211, mat212}, {0, mat222}}];
mm1110 = ArrayFlatten[{{mat211, mat212}, {mat221, 0}}];
(*Part 3*)
(*create a weighted digraph using "mat2"*)
nodes = {x1, x2, x3, x4, x5};
wam = select[mat2, .08, .12] /. {0. -> \[Infinity]};
wag = WeightedAdjacencyGraph[nodes, wam];
HighlightGraph[wag, Subgraph[wag, {x5, x3}], VertexLabels -> "Name",
GraphHighlightStyle -> "Thick"]
(*Part 4*)
Row[{
wam2 = select[mm1011, .08, .11] /. {0. -> \[Infinity]};
wag2 = WeightedAdjacencyGraph[nodes, wam2];
HighlightGraph[wag2, Subgraph[wag2, {x5, x3}],
VertexLabels -> "Name", GraphHighlightStyle -> "Thick"],
wam3 = select[mm1101, .08, .11] /. {0. -> \[Infinity]};
wag3 = WeightedAdjacencyGraph[nodes, wam3];
HighlightGraph[wag3, Subgraph[wag3, {x5, x3}],
VertexLabels -> "Name", GraphHighlightStyle -> "Thick"]
}]
It will be ideal if the weighted digraphs are created with fixed vertex coordinates
to make the comparison of the digraphs easy.
excLst
into two parts (that is, do you need to get 4 matrices in step 2)? $\endgroup$select[mat2, .08, .12]
beselect[mm0111, .08, .12]
? $\endgroup$nodes = {x1, x2, x3, x4, x5}
should benodes = {x5, x3, x1, x2, x4}
(to match the indices used to to getmat2
)? $\endgroup$parts12
in my answer below allows arbitrary number of parts. $\endgroup$select[mat2,.08, .12]
is correct (sorry for my mistake in the title of Part 3, which I edited the post) because withmat2
I wanted to see the graph of the full matrix without any zeros. $\endgroup$