TITLE: How to convert a random matrix to Unitary Matrix?
QUESTION [7 upvotes]: I know that a complex matrix $n \times n$ is said to be unitary if $AA^*=A^*A=I$ or equivalently if $A^*=A^{-1}$. But I asked what if there is a random matrix and we want to turn it into an unitary matrix, please also give an example.

REPLY [4 votes]: If you have access to Matlab or Octave, then producing such matrices is as easy as issuing the following commands:
n = 10;
A = rand(n) + 1i*rand(n);
[U,~] = qr(A);

I recommend that you use software such as this to generate these matrices in general. For small matrices that you would like to work by hand, the Gram-Schmidt procedure is what you want. Here is a simple example involving a real $3\times 3$ matrix. Let
$$
A = 
\begin{bmatrix}
1 & 1 & 1\\
1 & 0 & 2\\
0 & 2 & 1
\end{bmatrix}
$$
We are going to construct a $3\times 3$ unitary matrix $U$ from the columns of $A$. For simplicity I will first construct a matrix $\hat{U}$ with orthogonal columns, and then normalize at the end to get a unitary matrix. The first step is easy, we set the first column of $\hat{U}$ equal to the first column of $A$, i.e., $\hat{u}_1 = a_1$. The second column of $\hat{U}$ is equal to $a_2$ minus any contributions from $\hat{u}_1$, that is, 
$$
\hat{u}_2 = a_2 - \frac{\hat{u}_1\cdot a_2}{\hat{u}_1\cdot \hat{u}_1}\hat{u}_1 = 
\begin{bmatrix}
1\\
0\\
2
\end{bmatrix}
-
\frac{1}{2}
\begin{bmatrix}
1\\
1\\
0
\end{bmatrix}
=
\begin{bmatrix}
\phantom{-}1/2\\
-1/2\\
\phantom{-}2
\end{bmatrix}
$$
The last step is the same as the second except now we must remove from $a_3$ any contributions from $\hat{u}_1$ or $\hat{u}_2$. Thus,
\begin{align}
\hat{u}_3 &= a_3 - \frac{\hat{u}_1\cdot a_3}{\hat{u}_1\cdot \hat{u}_1}\hat{u}_1 - \frac{\hat{u}_2\cdot a_3}{\hat{u}_2\cdot \hat{u}_2}\hat{u}_2\\[1mm]
&=
\begin{bmatrix}
1\\
2\\
1
\end{bmatrix}
- \frac{3}{2}
\begin{bmatrix}
1\\
1\\
0
\end{bmatrix}
- \frac{1}{3}
\begin{bmatrix}
\phantom{-}1/2\\
-1/2\\
\phantom{-}2
\end{bmatrix}\\[1mm]
&=
\begin{bmatrix}
-2/3\\
\phantom{-}2/3\\
\phantom{-}1/3
\end{bmatrix}
\end{align}
The last step to obtain $U$ from $\hat{U}$ is to normalize the columns of $\hat{U}$. Doing so we obtain the unitary matrix
$$
U = 
\begin{bmatrix}
1/\sqrt{2} & \phantom{-}1/(3\sqrt{2}) & -2/3\\
1/\sqrt{2} & -1/(3\sqrt{2})          &\phantom{-}2/3\\
0          & \phantom{-}2\sqrt{2}/3   &\phantom{-}1/3
\end{bmatrix}
$$
As you can see from this simple example, the procedure is quite tedious by hand and is best left to a computer. You can read more about the Gram-Schmidt procedure here.