I have two list to process.Their first of each 2D part increasing monotonically and without repetition.
SeedRandom[1]
list1=Sort@Transpose[{RandomSample[Range@9,5],RandomSample[Range@9,5]}]
{{1,9},{2,1},{3,3},{6,4},{7,7}}
and
SeedRandom[2]
list2=Sort@Transpose[{RandomSample[Range@9,5],RandomSample[Range@9,5]}]
{{2,2},{4,1},{7,9},{8,3},{9,6}}
I want to inset some 2-dimension(such as $\{n,n\}$) list into place when the first element is incomplete.Like the two place where I have highlight it with red arrow
This is my solution for this
addElement[list_List] := Module[{pre},
pre = Array[{#, #} &, Max[First /@ list]];
Union[list,
Complement[pre, list, SameTest -> (Equal @@ First /@ {##} &)]]]
But I couldn't bear this ugly code.Can any elegant method do this?
permutations
,I will show I how to get this test list in my later edit. $\endgroup$With[{c = Complement[Range[1, #[[-1, 1]]], #[[All, 1]]]}, Union[#, Transpose[{c, c}]]] &
does what I think you're after, and much more quickly. $\endgroup$