The following algorithm will compare a given set of data from a predefined data set with only matrix operations without using loops
The algorithm will be explained using an example.
If the predefined dataset is given as $\mathcal{D}$ and the dataset given is $\mathcal{E}$,
$$\mathcal{D}=\begin{bmatrix} 0 &0& 0\\ 0& 0 &1\\ 0& 1 &0\\ 0& 1 &1\\ 1& 0& 0\\ 1& 0& 1\\ 1& 1& 0\\ 1& 1& 1 \\
\end{bmatrix} \qquad \mathcal{E}=\begin{bmatrix}
1 &0 &1 \\ 1 &1 &1\\ 1 &1 &1\\ 0 &0 &1\\ 0 &1 &0
\end{bmatrix}$$
Define
$$ \mathcal{S}=\mathcal{E}\cdot\mathcal{D}' $$
then
$$ \mathcal{S}=
\begin{bmatrix}
0& 1& 0& 1& 1& 2& 1& 2\\
0& 1& 1& 2& 1& 2& 2& 3\\
0& 1& 1& 2& 1& 2& 2& 3\\
0& 1& 0& 1& 0& 1& 0& 1\\
0& 0& 1& 1& 0& 0& 1& 1
\end{bmatrix}$$
Get the sum of the each row of $\mathcal{D}$ and $\mathcal{E}$
$$\mathcal{D}_{r\_sum}= \begin{bmatrix}
0\\
1\\
1\\
2\\
1\\
2\\
2\\
3\\
\end{bmatrix}
\qquad
\mathcal{E}_{r\_sum}=\begin{bmatrix}
2\\
3\\
3\\
1\\
1\\
\end{bmatrix}$$
Now define a new matrix with the same dimension as of $\mathcal{S}$ for each $\mathcal{E}_{r\_sum}$ and $\mathcal{D}_{r\_sum}$ as $\mathcal{E}_{sum\_mat}$ and $\mathcal{D}_{sum\_mat}$
$$\mathcal{E}_{sum\_mat}=[\mathcal{E}_{r\_sum} \mathcal{E}_{r\_sum} \cdots \mathcal{E}_{r\_sum}]$$
$$\mathcal{D}_{sum\_mat}=\begin{bmatrix}
\mathcal{D}_{r\_sum}' \\\mathcal{D}_{r\_sum}'\\ \vdots \\ \mathcal{D}_{r\_sum}'
\end{bmatrix}$$
For this example results will be,
$$\mathcal{E}_{sum\_mat}=\begin{bmatrix}
2& 2& 2& 2& 2& 2& 2& 2\\
3& 3& 3& 3& 3& 3& 3& 3\\
3& 3& 3& 3& 3& 3& 3& 3\\
1& 1& 1& 1& 1& 1& 1& 1\\
1& 1& 1& 1& 1& 1& 1& 1\\
\end{bmatrix}$$
$$\mathcal{D}_{sum\_mat}=\begin{bmatrix}
0& 1& 1& 2& 1& 2& 2& 3\\
0& 1& 1& 2& 1& 2& 2& 3\\
0& 1& 1& 2& 1& 2& 2& 3\\
0& 1& 1& 2& 1& 2& 2& 3\\
0& 1& 1& 2& 1& 2& 2& 3\\
\end{bmatrix}$$
Now compare each $\mathcal{E}_{sum\_mat}$ and $\mathcal{D}_{sum\_mat}$ with $\mathcal{S}$ separately and if each of $\mathcal{S}$ is equal to the elements of the two matrices the symbol 1 is recorded. The result of the example is given below
$$\mathcal{E}_{com}=\begin{bmatrix}
0& 0& 0& 0& 0& 1& 0& 1\\
0 & 0 & 0 & 0 & 0 & 0 & 0 & 1\\
0 & 0 & 0 & 0 & 0 & 0 & 0 & 1\\
0 & 1 & 0 & 1 & 0 & 1 & 0 & 1\\
0 & 0 & 1 & 1 & 0 & 0 & 1 & 1\\
\end{bmatrix}$$
$$\mathcal{D}_{comp}=\begin{bmatrix}
1& 1& 0 & 0& 1 & 1& 0 & 0\\
1 & 1 & 1 & 1 & 1 & 1 & 1 & 1\\
1 & 1 & 1 & 1 & 1 & 1 & 1 & 1\\
1 & 1 & 0 & 0 & 0 & 0 & 0 & 0\\
1 & 0 & 1& 0 & 0& 0 & 0& 0\\
\end{bmatrix}$$
From these two results at each element mark as 1 if and only if the both elements of each matrix is 1. We will name it as $\mathcal{C}$
$$\mathcal{C}=\begin{bmatrix}
0& 0 & 0& 0 & 0& 1 & 0& 0\\
0 & 0 & 0 & 0 & 0 & 0 & 0 & 1\\
0 & 0 & 0 & 0 & 0 & 0 & 0 & 1\\
0 & 1 & 0 & 0 & 0 & 0 & 0 & 0\\
0 & 0& 1 & 0& 0 & 0& 0 & 0\\
\end{bmatrix}$$
So the each column of $\mathcal{C}$ will denote the element of the predefined data set - $\mathcal{D}$ and the rows will represent each data point of $\mathcal{E}$. Element $c_{ij}$ of $\mathcal{C}$ will be 1 if $i^{th}$ data point of $\mathcal{E}$ is same as the $j^{th}$ data point of $\mathcal{D}$
The Matlab code for the above procedure is given below.
No comments:
Post a Comment