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

9k Views Asked by At

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.

10

There are 10 best solutions below

3
On BEST ANSWER

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.

1
On

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.

3
On

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

0
On

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.

2
On

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$

3
On

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

2
On

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.

0
On

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

1
On

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
On

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.