# Replace part in a matrix that matches specific pattern

I have the following problem. Given a matrix L which looks as follows,

Matrix L is a link, route matrix. Further, I got the following vector (d), which indicates the demand per vertex combination.

Now, I need to sort the following way. Each time that the first and last vertex in L match with one of the vertex pairs in vector d, I need to multiply the cell in the matrix with the value from the vector.

E.g. 1: The first cell in Matrix L (1--> 2,{1,2})=1 matches with row 1 in vector d. This is because {1,2}={1,2}. Therefore, this cell in L should be multiplied with the value 10 in d.

E.g. 2: The cell in matrix L at {1-->2, {1,3,2} also matches with the first row in d. So again this cell should be multiplied with 10

E.g. 3: The cell in matrix L at {1-->2, {1,3} matches with the second row in d. So it should be multiplied with 5.

So my requirement is that if the row in d matches with the first and last number in columns L, then I need to multiply the value in d with this specific cell in L.

Here is my code sample:

(*------------------------------------------------------------*)
(*-Function 0: generateBarabasiAlbertGraph--------------------*)
(*------------------------------------------------------------*)

generateBarabasiAlbertGraph[n_, k_] /; n >= 3 :=
Module[{g = CycleGraph[3], vc, vl, el},
Do[vc = VertexCount[g]; vl = VertexList[g];
(* Maps a new undirected edge from the new vertex =
vc +1 to the vertex obtained from the random sample of the Vertex \
Degrees    *)
el = Map[UndirectedEdge[vc + 1, #] &,
RandomSample[VertexDegree[g] -> vl, Min[k, vc]]];
(* generates a new graph by joing the edgeList of the previous \
graph with the new  *)
g = Graph[Join[EdgeList[g], el],
VertexLabels -> Placed["Name", Before]],
{n - 3}];
g
];

v = RandomInteger[{4, 4}];
l = RandomInteger[{1, 2}];
g = generateBarabasiAlbertGraph[v, l];
g = DirectedGraph[g];
vc = VertexCount[g];
vertices = VertexList[g];
edges = EdgeList[g];
vertexpairs = Permutations[vertices, {2}];
mdg = RandomInteger[{0, 10}, {vc, vc}];

paths = FindPath[g, Sequence @@ #, Infinity, All] & /@ vertexpairs;
paths = Flatten[paths, 1];
L = Outer[
Sort /@ Boole@MemberQ[DirectedEdge @@@ Partition[#2, 2, 1], #1] &,
edges, paths, 1];
Subscript[f, k] =
Partition[Part[mdg, Sequence @@ #] & /@ vertexpairs, 1];

(*Section. Print results*)
Print["L"];

Subscript[grid, 2] = Transpose@{Subscript[f, k]};
demand =
TableForm[Subscript[grid, 2],
TableHeadings -> {vertexpairs, {"demand per commocidty \
(\!$$\*SubscriptBox[\(f$$, $$k$$]\))", "demand round n"}}];
Print[demand];


Best,

Julian

• It's ALWAYS advisable to provide the code you've been working with. – Dr. belisarius Oct 23 '14 at 14:34