I want to take a list of list of integers and convert them to a matrix of 0s and 1s where an element of the matrix is 1 if "the row is in the column". The row corresponds to an integer in the list and should be ordered from 0 to the largest integer in the lists with no skips (i.e. there may be a row of zeroes). The columns corresponds to the inner lists and should be ordered in the order they appear in the list.
Is there a more efficient way to do this for large lists other than doing as I have done below? I want the function to handle ~100 integers, and ~20 inner lists, but the function may run hundreds of thousands of time as part of an optimisation routine, so an obvious time saving would be useful.
Example code:
MakeMatrix[ll_] :=
Block[{mat = Table[0, Max[Flatten[ll]] + 1, Length[ll]]},
Do[Do[mat[[row + 1, col]] = 1, {row, ll[[col]]}], {col, 1,
Length[ll]}]; Return[mat]]
MakeMatrix[{{0}, {0, 1}, {0, 1, 2}}]
MakeMatrix[{{0, 2}, {1, 2, 4}, {0, 1}}]
so the input:
{{0}, {0, 1}, {0, 1, 2}}
gives output:
{{1, 1, 1}, {0, 1, 1}, {0, 0, 1}}
And the input:
{{0, 2}, {1, 2, 4}, {0, 1}}
give output:
{{1, 0, 1}, {0, 1, 1}, {1, 1, 0}, {0, 0, 0}, {0, 1, 0}}