DifferenceI
DifferenceI
DifferenceI[ list1, list2 ] returns the indices of the elements of the first list that remain when the elements of the second list are removed.
Usages
DifferenceI[ list1, list2 ]
DifferenceI[ list1, list2 ] returns the positions (starting at 1) of the elements of list1 that are left after each element of list2 has removed one matching element. These are the positions of the elements that Difference would keep. Use it when you need to know where the remaining elements are, for example to select the matching elements of another list.
| Example | in | out |
|---|---|---|
g and the first a are removed; b (position 2) and the second a (position 4) remain. |
DifferenceI[ {a,b,g,a}, {g,a} ] |
{2, 4} |
Only g (position 3) remains. |
DifferenceI[ {a,b,g,a}, {a,b,c,d,e,a} ] |
{3} |
| Use the indices to look up the remaining elements. | L := {3, 8, 5, 8} |
I = {1, 3, 4}E = {3, 5, 8} |
Options
Method
Method sets when two elements are considered equal.
- Not set (default): elements must be the same expression, regardless of the order of terms or factors.
2.4and12/5are different. numerical: elements with the same numerical value are equal, such as2.4and12/5, orSqrt[4]and2.accuracy: like the default, but numbers with a limited precision, such asPrecisionConstant[3.14,2], are equal to a value within that precision. This is the same rule thatDifferenceuses.
| Syntax | Example | out |
|---|---|---|
DifferenceI[ list1, list2, Method=>numerical ] |
DifferenceI[ {2.4, 12/5, 3}, {12/5}, Method=>numerical ] |
{2, 3} |
DifferenceI[ list1, list2, Method=>accuracy ] |
DifferenceI[ {1+PrecisionConstant[3.14,2], 5}, {1+3.1415}, Method=>accuracy ] |
{2} |
Details
- Duplicates are counted separately: an element of
list2removes only one matching element oflist1, working from left to right. - Empty lists:
DifferenceI[ {1,2,3}, {} ]returns{1, 2, 3}andDifferenceI[ {}, {1} ]returns{}. - Exactly two lists: unlike
Difference,DifferenceIdoes not accept more than two lists.
| Example | in | out |
|---|---|---|
Without Method, 12/5 does not match 2.4. |
DifferenceI[ {2.4, 12/5, 3}, {12/5} ] |
{1, 3} |
| Invalid input | Result |
|---|---|
DifferenceI[ {a,b}, a ] |
Left unevaluated: the result is DifferenceI[ {a,b}, a ] itself. Both arguments must be lists. |
DifferenceI[ {1,2,3,4,5,6}, {2,4}, {5} ] |
Left unevaluated: only two lists are allowed. |
DifferenceI[ {1,2}, {1}, Foo=>True ] |
Error: Invalid associative argument for DifferenceI: Foo |
Warning
- Without the
Methodoption,DifferenceIcompares numbers with a limited precision differently fromDifference. UseMethod=>accuracyif the indices must correspond exactly to the result ofDifference. - With a single list,
DifferenceI[ {a,b} ]returns the list{a, b}itself, not a list of indices. - An unknown value for
Method(such as a typo) is ignored without an error, and the default comparison is used.
See also: Difference, IntersectI, RemoveI, Map