# Is there a math function to find an element in a vector?

I would like to write mathematically, if possible, the following statement:

Given a vector $x=[1,4,5,3]$ and an integer $j=3$, find the position of $j$ in $x$?

How to write this mathematically?

If I am looking for the position of the minimum value in $x$, I would achieve this by $\arg\min x$.

I guess $j^*=\operatorname{arg\,find} (x=j)$ but $\LaTeX$ does not recognize this.

• @Masacroso Or rather a comparison against each possible unidimensional projection in the original vector's coordinate system. Mar 30, 2016 at 16:52
• Us programmers would usually call it indexOf, if that helps. Mar 30, 2016 at 17:50
• @OrangeDog How does that help? I see no programming here. Apr 1, 2016 at 0:24
• I think in math you want to use the term '(finite) sequence' rather than 'vector'. (I doubt there's a standard mathematical notation for this, though, as I suspect it's a DP concept). Apr 1, 2016 at 15:26

Usually this is written as "Let $i$ be such that $x_i = j$." But that's not very compact. There isn't any standard notation for this that I know of. You could use $\arg \max_i [x_i = j ]$ which uses Iverson brackets to make things compact.

• That's a funny abuse of argmax, although I would say unreadable, the text version is much better. Mar 30, 2016 at 6:35
• One could instead use ​ max({i : x$_i$ = j}) ​ or ​ min({i : x$_i$ = j}) . ​ ​ ​ ​
– user57159
Mar 30, 2016 at 21:48
• Isn't that just "Einstein notation"? en.wikipedia.org/wiki/Einstein_notation
– Aron
Mar 31, 2016 at 4:59

If you want to talk about the $3$ in the vector $x=(1,2,3)$ then most people will just denote this element $x_3$ to indicate the third element of the vector.

If you are interested in the function that maps $x\to x_3$ then that function is denoted $\pi_3(x)$ and is called "the projection function (onto the third coordinate)". This function is pretty important in topology.

If you are interested in the function that tells you what index $k$ is, there isn't really a common notation for that because it's not necessarily a function. Besides, even when talking about it as a relation, in almost all circumstances if you know that $x$ contains a $3$, you can also just know which indices are $3$ and which are not as a consequences of knowing what x is

• It's not just "not a commonly used function", it isn't necessarily a function at all. For the vector $x = (3,2,3)$ this could return 1 or 3. You need to define it more to get a sensible result. Mar 30, 2016 at 17:17
• @Lacklub changed my comment there, thanks. Mar 30, 2016 at 19:51
• Actually, that is a very common (and important) "function" (correspondence) in computer science. We call it which. Mar 30, 2016 at 23:04

If you consider the vector $x$ as a function from $[1, n]$ to $\mathbb{N}$, you can use the inverse $x^{-1}$. See https://en.wikipedia.org/wiki/Inverse_function#Preimages.

In your example, if $x=(1,4,5,3)$ then $x^{-1} (\lbrace 3 \rbrace) = \lbrace 4 \rbrace$.

As Jonathan Gafar remarked, the inverse set might contain more than one element. Then you can use a minimum to get the first one.

For example, if $x=(1,2,5,7,2)$ then $x^{-1} (\lbrace 2\rbrace)= \lbrace 2, 5 \rbrace$ and $\min x^{-1} (\lbrace 2\rbrace) = 2$.

However, I don't recommend to use this notation without a proper introduction or definition, since the inverse symbol might be ambiguous.

• The inverse set can also be empty, so remember to handle or rule out that possibility before applying $\min$. Mar 30, 2016 at 13:06
• the minimum of an empty set is usually defined to be +infinity, so that should not be a problem. But you are right that you should not assume that you get a sensible index back, as the element may not have any pre-images. Apr 1, 2016 at 9:57

In my opinion you should use plain text or perhaps define such a function yourself if you need it frequently. Of course, if you have plain-text definition, you can still add a formula, if you feel that it will help the potential reader.

We define $\operatorname{arg\,find}(v,\alpha)$ to be the index of the first occurence of $\alpha$ in $v$ or to be equal to $\operatorname{length}(v)+1$ if there is no such index. $$\operatorname{arg\,find}(v,\alpha)=\min\{i \in \mathbb{N}^+\mid i>\operatorname{length}(v)\lor v_i=\alpha\}$$ where $v_i$ denotes the $i$-th coordinate of the vector $v$.

I hope this helps $\ddot\smile$

• I like the use of the one-past-the-last index to indicate that the vector does not contain the specified value. Mar 30, 2016 at 16:55
• Then it should be ${\rm arg\,first}(v,\alpha)$ Mar 31, 2016 at 17:01

Your question can be understood at least 2 ways.

(1) : you want a way to write this. I would suggest, given that there may be several such indices,

$$\{i : x_i=j\}.$$

If you can assume there is only one, then just let $i : x_i=j$.

(2) : you want to define a new notation. Everything is possible, one could for instance define a position function:

$$\text{pos}(j,x):=\min\{i : x_i=j\}.$$

• +1 this is the proper answer. Mar 31, 2016 at 8:08

A function $f$, to find the position of an integer $j$, within in a vector $x$ (of size $n$).

In the case that the integer $j$, may only occur once in the vector $x$:

The desired function can be composed of a sum $\sum$, and the delta function $\delta$.

Where the delta function is defined as $\quad\delta(a) := \begin{cases}0&a\neq0\\1&a=0\end{cases}$

$$f(x,j) = \sum_{i=1}^ni\delta(x_i-j)$$ (If zero is returned from this function, the vector does not include an element equal to $j$)

$$x=[1,4,5,3], \quad j =3$$

$$f\big([1,4,5,3], 3\big) = 1\delta(x_1-3) + 2\delta(x_2-3) + 3\delta(x_3-3)+ 4\delta(x_4-3)$$ $$=1(0)+2(0)+3(0)+4(1)$$ $$=4$$

In the case that the integer $j$, may occur more than once, and all occurrences are to be found (as a set):

The desired function can be composed of a union $\cup$, and the delta function $\delta$.

$$f(x,j) = \bigcup_{i=1}^ni\delta(x_i-j)\setminus 0$$ (If the empty set is returned from this function, $x$ does not include the element $j$)

$$x = [3,4,5,3], \quad j = 3$$

$$f\big([3,4,5,3], 3\big) = 1\delta(x_1-3) \cup 2\delta(x_2-3) \cup 3\delta(x_3-3) \cup 4\delta(x_4-3)\setminus 0$$ $$= 1(1)\cup2(0)\cup3(0)\cup4(1)\setminus 0$$ $$= \{1,0,4\}\setminus 0$$ $$= \{1,4\}$$

• A perfect way to obscure your intention! Mar 30, 2016 at 8:47
• Very nice and clever solution, if not obfuscated. ;) Mar 30, 2016 at 13:50
• Clever and neat, but gives... interesting results when the value occurs more than once (ie. gives the sum of indices). +1 for returning 0 when no match exists Mar 30, 2016 at 17:24

So what you mean is, given some argument, return the component of the vector which contains that argument? So if $x = (1,3,4,2,6)$, then you would ask to find the position of $4$, and the result would be the third component of $x$?

Note that such a function would not be well-defined (if defined on all $n$-tuples). For example, if $x = (1,2,5,7,2)$, and you asked to find the position of $2$, then would the function return the second component or the fifth component? You would have to make a choice.

• it is also not defined if the vector does not contain the value of course
– jk.
Mar 30, 2016 at 10:20

If you tell the reader to think of $x$ as a function, you can denote this $x^{-1}(j).$

More explicitly, let $\underline{4} = \{0,1,2,3\}$. Tell the reader to think of $x$ as a function $x : \underline{4} \rightarrow \mathbb{Z}$. Then the set of all $i \in \underline{4}$ satisfying $x(i) = j$ is denoted $x^{-1}(j)$.

Vector dot products do this. If you want the second element of a vector $X$, then $X \cdot \langle0, 1, 0, 0, 0\rangle$will give you that component if $X$ is a five-dimensional vector.

• That isn't what the question is asking for. We're not trying to find the value in a particular position, but the position of a particular value. Mar 31, 2016 at 3:21

You could write code that does this, if you'll consider that a "mathematical function."

Write a $for$ loop which iterates over the elements of the array, checking if the position at the loop index is the same as the element you want to find. If you find an element where there is a match, break out of your loop. If not, return that no such element exists in the array.