1
$\begingroup$
Intersection[Table[Table[Sqrt[p1]/r1,{r1,0.1,0.7,0.1}],{p1,0.5,5,0.5}]]

I am trying to find same numbers in the sublists. But Intersection is not working for this...

$\endgroup$
6
  • $\begingroup$ What if you use exact numbers (e.g. 1/10) instead of inexact ones (e.g. 0.1)? $\endgroup$ Jul 31, 2015 at 21:12
  • $\begingroup$ Still not working...I think the curly bracket is creating problem. $\endgroup$
    – santosh
    Jul 31, 2015 at 21:20
  • 1
    $\begingroup$ You use the wrong syntax for Intersection. See the difference between Intersection[{0, 1, 3}, {0, 10, 2}, {3, 0}] (good) and Intersection[{{0, 1, 3}, {0, 10, 2}, {3, 0}}] (bad). In the second case (your case) it works if you write instead Apply[Intersection, {{0, 1, 3}, {0, 10, 2}, {3, 0}}] or Intersection @@ {{0, 1, 3}, {0, 10, 2}, {3, 0}}. However in your case it seems the intersection is empty ! $\endgroup$
    – SquareOne
    Jul 31, 2015 at 21:37
  • $\begingroup$ How will it give a sorted list of the elements common in any two sublists? $\endgroup$
    – santosh
    Jul 31, 2015 at 23:21
  • $\begingroup$ santosh, your last comment seems to ask a different question. Do you want elements common to any two sublists, or elements common to all sublists? I understood your original question to be the latter. Please clarify. $\endgroup$
    – MarcoB
    Jul 31, 2015 at 23:39

2 Answers 2

3
$\begingroup$

Does this work:

Apply[
  Intersection[#, SameTest -> (EuclideanDistance[#1, #2] < 10^(-5) &)] &,
  Table[Table[Sqrt[p1]/r1, {r1, 0.1, 0.7, 0.1}], {p1, 0.5, 5, 0.5}]
]

(* gives: {1.01015, 1.17851, 1.41421, 1.76777, 2.35702, 3.53553, 7.07107} *)

You can probably adjust the numerical tolerance (10^-5) as needed.

$\endgroup$
1
  • $\begingroup$ It gives first list {7.07107, 3.53553, 2.35702, 1.76777, 1.41421, 1.17851, 1.01015}. Please check $\endgroup$
    – santosh
    Jul 31, 2015 at 21:45
0
$\begingroup$

I think you're after something like this:

ReplaceList[myListofLists, {a : ___, {b : ___, x_, ___}, c : ___, {d : ___, x_, ___}, ___} :> 
    {{Length@{a} + 1, Length@{b} + 1}, {Length@{a} + Length@{c} + 2, Length@{d} + 1}, x}]

(*
{{{1, 1}, {4, 2}, 7.07107}, {{1, 2}, {4, 4}, 3.53553}, 
 {{1, 3}, {4, 6}, 2.35702}, {{2, 1}, {8, 2}, 10.}, 
 {{2, 2}, {8, 4}, 5.}, {{2, 3}, {8, 6}, 3.33333}}
*)

Gives list with elements of {{Position of first list, position in that list},{position of second list, position in that list},number}... the coordinates can be converted to the values of p1 and r1 by multiplication by chosen increment and addition of chosen initial values for the table iterators.

If all you care about are the actual values,

Union @@ Intersection @@@ Subsets[myListofLists, {2}]

will suffice.

$\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.