2
$\begingroup$

I have two lists of the form:

list1 = {{1, 2.003}, {3, 2.01}, {2, 1.0011}};
list2 = {{2, 1.001}, {3, 2.0034}, {2, 1.0001}};

I want to do the following:

Get the absolute value of subtracting each two second sub-elements in the two lists from each other. For example {1, 2.003} from list1 and {3, 2.0034} from list2. $|2.003-2.0034|$ and if it is in the range from 0.0001 to 0.001 subtract their corresponding first items i.e. $|1-3|$ in absolute value also.

How to do that for all the elements in the two lists in Mathematica?

$\endgroup$
3
$\begingroup$

I am guessing that you want something like the following:

To do something to each pair of elements from list1 and list2 you can use Outer:

table = Outer[Module[{d = Abs @ Subtract @ ##}, 
    If[0.0001 <= d[[2]] <= 0.001, d, d[[2]]]] &, list1, list2,  1]; 

Add list1 and list2 as row and column headers and display using Grid:

Grid[Prepend[Join[List /@ list1, table, 2], Prepend[list2, ""]],
  Alignment -> {Center, Center}, 
  Dividers -> {{1 -> Black, -1 -> Black, 2 -> Gray}, {1 -> Black, -1 -> Black, 2 -> Gray}}]

enter image description here

$\endgroup$
  • 2
    $\begingroup$ That is exactly what I want , thank you very much. Appreciate the Help. $\endgroup$ – Rami1234 Jul 27 '19 at 17:54
  • $\begingroup$ @Rami1234, glad it worked for you. Welcome to mma.se. $\endgroup$ – kglr Jul 27 '19 at 17:56
  • $\begingroup$ Can you help to only get those who satisfy the condition as the table is very big, it has about 700 rows. $\endgroup$ – Rami1234 Jul 27 '19 at 18:10
  • $\begingroup$ @Rami1234, try table2 = Outer[Module[{d = Abs @ Subtract @ ##}, If[0.0001 <= d[[2]] <= 0.001, d, Nothing]] &, list1, list2, 1]; $\endgroup$ – kglr Jul 27 '19 at 18:56
  • $\begingroup$ it gave this \begin{array}{|c|ccc} \hline \text{} & \{3,2.0034\} & \{2,1.001\} & \{2,1.0001\} \\ \hline \{1,2.003\} & \{2,0.0004\} & \text{} & \text{} \\ \{3,2.01\} & \text{} & \text{} & \text{} \\ \{2,1.0011\} & \{0,0.0001\} & \text{} & \text{} \\ \end{array} $\endgroup$ – Rami1234 Jul 27 '19 at 19:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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