1
$\begingroup$

I have the following problem. I have two lists:

list1 defines all possible commodities in a network g. E.g. Form vertex 1 to vertex 2.

list1 = {{1,2},{1,3},{1,4},....}

list2 defines all possible routes from vertex x to vertex y. E.g.:

  • r1 could be from vertex 1 to vertex 2 and the route would be 1,2
  • r2 then could be 1,3,2 etc

For example:

list2 = {{1,2},{1,3,2},{1,4,3,2},{},{},{}}

Now, I would like to construct a commodity-route incidence matrix H, which will be 1 if e.g. the elements from List one represent the first and the last element of list2.

Any ideas?

$\endgroup$
  • $\begingroup$ Unless I'm misunderstanding what you wrote, the problem should be trivial, since the commodity-route incidence matrix H will consist of a single row of ones, and zeros everywhere else. The location of this single row of ones will be the index of {x,y} in list1. Thus, no computation is needed at all, since you already chose x and y in the construction of list2, which furthermore makes the computation of list2 redundant. $\endgroup$ – DumpsterDoofus Oct 18 '14 at 13:19
2
$\begingroup$

Here is one possible way to do it:

L1 = {{1, 2}, {1, 3}, {1, 4}};
L2 = {{1, 2}, {1, 3, 2}, {1, 4, 3, 2}};
Outer[Boole[#1 == #2[[{1, -1}]]] &, L1, L2, 1]

which produces

$$\left( \begin{array}{ccc} 1 & 1 & 1 \\ 0 & 0 & 0 \\ 0 & 0 & 0 \\ \end{array} \right),$$

which shows that the computation of L2 is redundant, since the position of the row of ones is uniquely determined by the initial choice of x and y.

$\endgroup$
  • $\begingroup$ Works perfectly. $\endgroup$ – Julian Oct 18 '14 at 13:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.