4
$\begingroup$

I have two lists of numbers of the same length. I want to display the numbers in a table with two columns, one list in each column. I also want to drawn lines between any number in the first column and any matching number in the second column. How can I do that?

Here is my code so far:

SeedRandom[10];
MatrixForm[
  Join[Transpose[{RandomInteger[{1, 20}, 10]}], 
  Transpose[{RandomInteger[{1, 20}, 10]}], 2], 
  TableSpacing -> {1, 20}]

Here is the way I would like my output to look:

Sample list with matches

$\endgroup$
4
  • 4
    $\begingroup$ Poor quality, too-localized question. $\endgroup$ Sep 16, 2015 at 19:24
  • 4
    $\begingroup$ I think you are over-reacting, guys. The question could be saved with a little editing, and I think it's part of our job to save it if we can. I have have put my typing fingers to work in this regard, and now I recommend this question be reopened. $\endgroup$
    – m_goldberg
    Sep 16, 2015 at 20:41
  • $\begingroup$ @m_goldberg you may be right. There are a very large number of bad questions being submitted lately and sometimes the urge to close wins out. I still think that this one lacks motivation and is too localized, albeit it is more pleasant to read after your ministrations. I retracted my downvote. $\endgroup$ Sep 17, 2015 at 1:36
  • $\begingroup$ @OleksandrR. I agree with your assessment of the large number of bad questions. Happens every fall when the academic year begins, doesn't it? $\endgroup$
    – m_goldberg
    Sep 17, 2015 at 1:49

2 Answers 2

13
$\begingroup$
SeedRandom[10];

col1 = RandomInteger[{1, 20}, 10];
col2 = RandomInteger[{1, 20}, 10];

txt = {
   n = Length[col1];
   Text[ToString[#], {1, n--}, {1.5, 0}] & /@ col1,
   n = Length[col2];
   Text[ToString[#], {2, n--}, {-1.5, 0}] & /@ col2};

lines = Cases[
   Outer[
    If[#1[[1]] == #2[[1]],
      Line[{#1[[2]], #2[[2]]}],
      Sequence[]] &, Sequence @@ txt],
   Line[_], Infinity];

Graphics[{txt, Red, Thick, lines},
 ImageSize -> 300,
 AspectRatio -> 1/GoldenRatio]

enter image description here

$\endgroup$
1
  • $\begingroup$ Add colored lines to show direction of placement between the two lists colored = Table[Which[ lines[[i, 1, 1, 2]] == lines[[i, 1, 2, 2]], {Blue, lines[[i]]}, lines[[i, 1, 1, 2]] > lines[[i, 1, 2, 2]], {Green, lines[[i]]}, lines[[i, 1, 1, 2]] < lines[[i, 1, 2, 2]], {Red, lines[[i]]}] , {i, Length[lines]}]; Graphics[{txt, colored}, ImageSize -> 350, AspectRatio -> 2] $\endgroup$
    – ex-kiwi
    Sep 17, 2015 at 19:00
0
$\begingroup$
ClearAll[f1]
f1 = Module[{lst = Join[##], l = Length@#, vertices, edges}, 
    vertices = Labeled[##, Center] & @@@ Transpose[{Range@Length@#, #} &@lst];
    edges = Join @@ Outer[If[#[[2]] == #2[[2]], UndirectedEdge[#[[1]], #2[[1]]], ## &[]]&, 
     vertices[[;; l]],  vertices[[1 + l ;;]]];
    Graph[vertices, edges, VertexSize -> {"Scaled", .05}, 
     VertexShapeFunction -> { _ -> None, Alternatives@@Flatten[List@@@edges] -> "Circle"}, 
     VertexLabelStyle -> {_ -> 14, Alternatives@@Flatten[List@@@edges] -> {Red , 15}}, 
     VertexCoordinates -> Join[Thread[{0, Range[l]/l}], Thread[{1, Range[l]/l}]]]] &;

Example:

SeedRandom[10];
col1 = RandomInteger[{1, 20}, 10];
col2 = RandomInteger[{1, 20}, 10];

f1[col1, col2]

enter image description here

$\endgroup$

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.