2
$\begingroup$

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}}

$\endgroup$
2
  • $\begingroup$ 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}} ? $\endgroup$
    – kglr
    Commented Apr 21, 2020 at 8:53
  • $\begingroup$ @kglr there are 3 missing entries for the binary elements. So, there should be $2^3$ cases. $\endgroup$
    – Andeanlll
    Commented Apr 21, 2020 at 8:55

2 Answers 2

2
$\begingroup$

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}}
$\endgroup$
3
$\begingroup$

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}}}
$\endgroup$
3
  • $\begingroup$ 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. $\endgroup$
    – Andeanlll
    Commented Apr 21, 2020 at 6:49
  • $\begingroup$ You should put that example in your question @Andean, that requires something completely different. $\endgroup$ Commented Apr 21, 2020 at 7:09
  • $\begingroup$ @J.M., Yeah, I should've done that from the beginning. I've updated the question. $\endgroup$
    – Andeanlll
    Commented Apr 21, 2020 at 7:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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