# Problem converting matlab formula for relative frequency vector to mathematical formula

I have the following matlab code:

range = 7;
vector = [1 2 3 3 5 6 7 5 4];
freq = zeros(1,range);

for i=1:range freq(i) = length(vector(vector==i))/length(vector); end


I'd like to transform it into a mathematical formula but need help with the numerator and the sequence.

This is my attempt:

$v = vector$

$m = range$

s should be the subset of v equal to the current integer

$f = \left|s \subseteq v | \right| / \left|{v}\right|$

and this also needs to include the sequence of all integers from:

$\{1,\ldots\,m\}$

I'm just not sure how to put it together.

-

Let $\delta_i$ be the Kronecker delta operator. Let $n$ represent the dimensionality of the vector and let $m$ represent the range. Consider the $m \times n$ matrix:

$$D_{m,n} = \frac{1}{n}\left( \begin{array}{cccc} \delta_1 & \delta_1 & \dots & \delta_1 \\ \delta_2 & \delta_2 & \dots & \delta_2 \\ \vdots & & \ddots & \vdots \\ \delta_m & \delta_m & \dots & \delta_m \end{array}\right)$$

Then $D_{m,n} \cdot v$ is the output you desire. Let us look at an example for the values you stated:

\begin{align} D_{m,n} \cdot v & = \frac{1}{4}\left( \begin{array}{cccc} \delta_1 & \delta_1 & \delta_1 & \delta_1 \\ \delta_2 & \delta_2 & \delta_2 & \delta_2 \\ \delta_3 & \delta_3 & \delta_3 & \delta_3 \end{array}\right) \left(\begin{array}{c} 1 \\ 2 \\ 2\\ 3 \end{array}\right) \\ &= \frac{1}{4}\left(\begin{array}{c} \delta_11 + \delta_1 2 + \delta_1 2+ \delta_1 3 \\ \delta_21 + \delta_2 2 + \delta_2 2+ \delta_2 3 \\ \delta_3 1 + \delta_3 2 + \delta_3 2+ \delta_3 3 \end{array}\right) \\ & = \frac{1}{4} \left(\begin{array}{c} 1 + 0 + 0 + 0 \\ 0 + 1 + 1 + 0 \\ 0 + 0 + 0 + 1 \end{array}\right) \\& = \frac{1}{4} \left(\begin{array}{c} 1 \\ 2 \\ 1 \end{array}\right)\end{align}

You can write this in a nicer way by:

$$D_{m,n} \cdot v = \frac{1}{n} \left(\begin{array}{c} \delta_1 \\ \delta_2 \\ \vdots \\ \delta_m \end{array}\right) \left(\begin{array}{c} 1 \\ 1 \\ \vdots \\ 1 \end{array}\right)^T v$$

but be careful you understand that this is just shorthand and you need to know when it's an operator action and when it's multiplication.

-
Using the notation from here. $\delta_{i} = \begin{cases} 0, & \mbox{if } i \ne 0 \\ 1, & \mbox{if } i=0 \end{cases}$ What would the first Kronecker delta be if v = [1,2,3], m=3, n=3 –  dsimmie Mar 14 '13 at 11:30
Here I am treating $\delta_i$ as a function $\delta_i(j) = \delta_i^j$ which is $1$ if $j=i$ and $0$ otherwise –  muzzlator Mar 14 '13 at 11:36
In that case you mentioned, we'd have $\frac{1}{3} (\delta_1 \delta_2 \delta_3)^T (1, 1, 1) v$. Just be a bit careful, in the "nicer" way of writing it, the distinction between multiplication and application of the $\delta_i$ operator is blurred. You may prefer the original definition of $D_{m,n}$ –  muzzlator Mar 14 '13 at 11:39
Thanks for your time. I am probably doing something wrong but I can not get this work out for me with a trivial example. Let $n=4, m=3, \textbf{v}= \left( \begin{array}{c} 1 \\ 2 \\ 2 \\ 3 \end{array}\right)$ $DN = \frac{1}{n}D$ $$DN = \left( \begin{array}{cccc} 0.25 & 0 & 0 & 0 \\ 0 & 0.25 & 0 & 0 \\ 0 & 0 & 0.25 & 0 \end{array}\right)$$ $$f = DN\textbf{v} = \left( \begin{array}{c} 0.25 \\ 0.5 \\ 0.5 \end{array}\right)$$ This is not the relative frequency I expect, it should be: $$f = DN\textbf{v} = \left( \begin{array}{c} 0.25 \\ 0.5 \\ 0.25 \end{array}\right)$$ –  dsimmie Mar 14 '13 at 14:24
I think it's the problem with my notation, the $\delta_i$ terms aren't numbers. I'll edit my answer with an example. –  muzzlator Mar 14 '13 at 14:44
show 1 more comment