# Augmenting a matrix by filling missing entries

Suppose I have a $$2\times 4$$ matrix like the following:

sampledata = {{1, , 1, 0}, {0, 1, , 1}};


It's a binary zero or one entry matrix. And as you can see, the cell {1, 2} and {2, 3} are empty.

What I want to do is to fill up the missing entries with all possible combinations of zeroes and ones.

So, the final matrix should look like

updateddata = {{1, 1, 1, 0},{1, 0, 1, 0}, {0, 1, 1, 1}, {0, 1, 0, 1}};


How should I proceed to get the augmented matrix of the final form?

Thank you for your comments. But I found the functions suggested works only when there are at most one missing entries in each row..

I should update my example. The matrix is

sampledata1 = {{1,,,0},{0,,1,1}}

That has two missing entries in the first row and one missing entry in the second row. So there are $$2^3$$ possibilities to fill in the missing entries that makes the final matrix look like

updateddata1 = {{1,1,1,0},{0,1,1,1},{1,1,1,0},{0,0,1,1},{1,1,0,0},{0,1,1,1},{1,1,0,0},{0,0,1,1},{1,0,1,0},{0,1,1,1},{1,0,1,0},{0,0,1,1},{1,0,0,0},{0,1,1,1},{1,0,0,0},{0,0,1,1}}

• shouldn't updateddata1 be {{1, 1, 1, 0}, {1, 1, 0, 0}, {1, 0, 1, 0}, {1, 0, 0, 0}, {0, 1, 1, 1}, {0, 1, 0, 1}} ?
– kglr
Commented Apr 21, 2020 at 8:53
• @kglr there are 3 missing entries for the binary elements. So, there should be $2^3$ cases. Commented Apr 21, 2020 at 8:55

There's a bunch of ways to do it, but I find this the easiest to parse:

Flatten[Table[Replace[m, Null -> b, 1],
{m, {{1, , 1, 0}, {0, 1, , 1}}}, {b, {1, 0}}], 1]
{{1, 1, 1, 0}, {1, 0, 1, 0}, {0, 1, 1, 1}, {0, 1, 0, 1}}


The OP's new example requires a slightly different approach:

sample = {{1, , , 0}, {0, , 1, 1}};
pos = Position[sample, Null];

Flatten[Table[ReplacePart[sample, Thread[pos -> t]], {t, Tuples[{1, 0}, {3}]}], 1]
{{1, 1, 1, 0}, {0, 1, 1, 1}, {1, 1, 1, 0}, {0, 0, 1, 1}, {1, 1, 0, 0},
{0, 1, 1, 1}, {1, 1, 0, 0}, {0, 0, 1, 1}, {1, 0, 1, 0}, {0, 1, 1, 1},
{1, 0, 1, 0}, {0, 0, 1, 1}, {1, 0, 0, 0}, {0, 1, 1, 1}, {1, 0, 0, 0},
{0, 0, 1, 1}}


Update:

ClearAll[replaceNulls]
replaceNulls = Module[{pos = Flatten@Position[#, Null], lst = #},
Table[SubsetMap[t &, lst, pos], {t, Tuples[{1, 0}, Length@pos]}]] &;


Examples:

sampledata = {{1, , 1, 0}, {0, 1, , 1}};

Catenate @ Map[replaceNulls] @ sampledata


. {{1, 1, 1, 0}, {1, 0, 1, 0}, {0, 1, 1, 1}, {0, 1, 0, 1}}

sampledata2 = {{1, , , 0}, {0, 1, , 1}} ;

Catenate @ Map[replaceNulls] @ sampledata2

{{1, 1, 1, 0}, {1, 1, 0, 0}, {1, 0, 1, 0}, {1, 0, 0, 0},
{0, 1, 1, 1}, {0, 1, 0, 1}}


Original answer: Works when each input list has a single Null:

We can use ReplaceAll with a list of lists of rules:

ReplaceAll[{{Null -> 1}, {Null -> 0}}] /@ sampledata

{{{1, 1, 1, 0}, {1, 0, 1, 0}}, {{0, 1, 1, 1}, {0, 1, 0, 1}}}

Join @@ %  (* or Catenate @ % *)

{{1, 1, 1, 0}, {1, 0, 1, 0}, {0, 1, 1, 1}, {0, 1, 0, 1}}


Or combine the two steps:

ClearAll[f]
f = Catenate @* Map[ReplaceAll[{{Null -> 1}, {Null -> 0}}]];

f @ sampledata

{{1, 1, 1, 0}, {1, 0, 1, 0}, {0, 1, 1, 1}, {0, 1, 0, 1}}


More generally,

vlist = {v1, v2, v3};

ReplaceAll[List /@ Thread[Null -> vlist]] /@ sampledata

 {{{1, v1, 1, 0}, {1, v2, 1, 0}, {1, v3, 1, 0}},
{{0, 1, v1, 1}, {0, 1, v2, 1}, {0, 1, v3, 1}}}

• Thanks. But I found this function only works when there is at most one missing entry in each row.. For example, when sampledata = {{1, , , 0}, {0, 1, , 1}};, it still produces {{{1, 1, 1, 1}, {1, 0, 0, 1}}, {{0, 1, 1, 1}, {0, 1, 0, 1}}} that has only 4 rows.. instead of 8 rows that contains all possible combinations of zeroes and ones for the missing entries. Commented Apr 21, 2020 at 6:49
• You should put that example in your question @Andean, that requires something completely different. Commented Apr 21, 2020 at 7:09
• @J.M., Yeah, I should've done that from the beginning. I've updated the question. Commented Apr 21, 2020 at 7:11