Wolfram Research

Function Repository Resource:

CorrespondingIntegers

Source Notebook

Create a list of positive integers containing as many elements as the expression to which it is applied

Contributed by: Seth J. Chandler

ResourceFunction["CorrespondingIntegers"][expr]

gives a list {1n} of positive integers where n is the length of expr.

ResourceFunction["CorrespondingIntegers"][expr,start,jump]

gives a list {start,start+jump,,start+(n-1)×jump} where n is the length of expr.

ResourceFunction["CorrespondingIntegers"][start,jump]

creates an operator that, when applied to an expression expr, gives a list {start,start+jump,,start+(n-1)×jump} where n is the length of expr.

Examples

Basic Examples

Create a list of the positive integers corresponding to letters “a” to “j”:

In[1]:=
ResourceFunction["CorrespondingIntegers"][CharacterRange["a", "j"]]
Out[1]=

Create a list of integers starting with 5 and incrementing by 2 each time that correspond to the letters "a" to "j":

In[2]:=
ResourceFunction["CorrespondingIntegers"][CharacterRange["a", "j"], 5,
  2]
Out[2]=

Create an operator which, when it an expression, creates a list with the same number of parts as that expression, in which the first element is 1, and in which each successive value is 1 higher:

In[3]:=
ResourceFunction["CorrespondingIntegers"][1, 1][
 CharacterRange["a", "j"]]
Out[3]=

Scope

The function works on expressions that are not lists :

In[4]:=
ResourceFunction["CorrespondingIntegers"][a[b, c, d]]
Out[4]=

Because the length of an Association is equal to its number of key value pairs, the function can be sensibly applied to associations:

In[5]:=
ResourceFunction["CorrespondingIntegers"][
 Association["a" -> 6, "b" -> 9]]
Out[5]=

The function looks only at the first level of an expression to determine the correspondence :

In[6]:=
ResourceFunction["CorrespondingIntegers"][IdentityMatrix[7]]
Out[6]=

Using non - integer values for the starting value and/or the jump will give a warning, as this is not the purpose for which the function is intended:

Out[7]=

Applications

Use CorrespondingIntegers to develop indices into a training and test set used for machine learning:

In[8]:=
SeedRandom[1]; Module[{allIndices = ResourceFunction[
    "CorrespondingIntegers"][{<|"sex" -> "female", "survived" -> True|>, <|"sex" -> "male", "survived" -> True|>, <|"sex" -> "female", "survived" -> False|>, <|"sex" -> "male", "survived" -> False|>, <|"sex" -> "female", "survived" -> False|>, <|"sex" -> "male", "survived" -> True|>, <|"sex" -> "female", "survived" -> True|>, <|"sex" -> "male", "survived" -> False|>, <|"sex" -> "female", "survived" -> True|>, <|"sex" -> "male", "survived" -> False|>}], trainingIndices, testIndices},
 trainingIndices = RandomSample[allIndices, 7];
 testIndices = Complement[allIndices, trainingIndices];
 {trainingIndices, testIndices}]
Out[8]=

Index the terms in some document both backward and forward:

In[9]:=
Module[{vocabulary = DeleteDuplicates[
    TextWords[ExampleData[{"Text", "USConstitution"}]]],
  correspondence, threaded},
 correspondence = ResourceFunction["CorrespondingIntegers"][vocabulary];
 threaded = Thread[vocabulary -> correspondence];
 {Dispatch[threaded], Dispatch[Reverse /@ threaded]}
 ]
Out[9]=

Properties and Relations

This function can be combined with Thread to emulate the action of MapIndexed at level 1 where one takes the first part of the index:

In[10]:=
MapIndexed[f[#1, #2[[1]]] &, {a, b, c}, 1] === With[{expr = {a, b, c}}, Thread[f[expr, ResourceFunction["CorrespondingIntegers"][expr]]]]
Out[10]=

Resource History

License Information