[Tex/LaTex] writing an algorithm in a table

algorithms

I want to write an algorithm in a table for my paper. I tried to write it, but I want to write ''While'' instead of ''for'' in this algorithm. How can I do it? And I want to put the table everywhere in the text.

\begin{algorithm}
    \caption{
OMP Algorithm
    }
    \label{alg1}
    \begin{latin}
        \begin{algorithmic}[1]
            \REQUIRE Decomposition of signal ‎$x‎$‎
            \STATE\textbf{Input:}  signal ‎$‎x \in \mathcal{R}^{m}‎$‎, Dictionary ‎$‎‎\mathcal{G}‎ ‎\in ‎\mathcal{R}^{m \times n‎‎}‎$‎,‎‎$‎\hat{x}=‎\emptyset‎$‎ ‎‎
            \STATE  \textbf{Output:}  Decomposed signal $‎\hat{x}_{est}‎‎$ ‎after ‎$‎k‎$‎ ‎‎iteration, Residual ‎‎$‎R^{(k)}‎$‎‎
      \STATE ‎\textbf{Initialization} ‎$‎‎R^{(0)}=x‎‎$‎
            \STATE \textbf{for i=1:k do}‎
            \STATE ‎$‎l‎=\displaystyle{arg\max_{l=1,\dots ,l} |‎\langle ‎g_l,R^{(i)} ‎‎\rangle‎‎|‎}$‎‎‎ ‎\qquad‎ ‎finding ‎the ‎atom ‎in ‎dictionary ‎‎$‎\mathcal{G}‎$‎ ‎‎with ‎maximum ‎correlation ‎with ‎residual.‎
            \STATE ‎$‎R^{(i+1)}=R{(i)}-a_l g_l^{(i)}‎‎$‎
            \STATE ‎$\hat{x}‎=‎\hat{x}‎+‎\langle ‎R^{(i)},g_{l}^{(i)} ‎‎\rangle ‎g_{l}^{(i)}‎‎$‎
      \STATE \‏‎textbf{end}‎
        \end{algorithmic}
    \end{latin}
\end{‎algorithm}

algorithm

Best Answer

There are more tools at your disposal inside the algorithmic environment:

enter image description here

\documentclass{article}
\usepackage{algorithm,algcompatible,amsmath}
\DeclareMathOperator*{\argmax}{\arg\!\max}% https://tex.stackexchange.com/q/83169/5764
\algnewcommand\INPUT{\item[\textbf{Input:}]}%
\algnewcommand\OUTPUT{\item[\textbf{Output:}]}%

\begin{document}

\begin{algorithm}
    \caption{OMP Algorithm}
  \begin{algorithmic}[1]
    \REQUIRE Decomposition of signal $x$
    \INPUT signal $x \in \mathcal{R}^{m}$, Dictionary $\mathcal{G} \in \mathcal{R}^{m \times n‎‎}$, $\hat{x} = \emptyset$
    \OUTPUT Decomposed signal $\hat{x}_{\text{est}}$ after $k$ iteration, Residual $R^{(k)}$
    \STATE \textbf{Initialization} ‎$R^{(0)} = x$
    \WHILE{$i \leq k$}
      \STATE $l = \displaystyle \argmax_{l = 1,\dots,l} |\langle g_l,R^{(i)} \rangle|$
        \COMMENT{finding the atom in dictionary $\mathcal{G}$ ‎with maximum correlation with residual.}
      \STATE $R^{(i+1)} = R{(i)}-a_l g_l^{(i)}‎$
      \STATE $\hat{x} = \hat{x}+\langle R^{(i)}, g_{l}^{(i)} \rangle g_{l}^{(i)}‎$
      \STATE $i = i + 1$
    \ENDWHILE
  \end{algorithmic}
\end{algorithm}

\end{document}

algcompatible (from algorithmicx) provides \WHILE{<condition>} and \ENDWHILE. As you can see, it also indents the statement to show it's (nested) structure.

For completeness, I've also defined \INPUT and \OUTPUT, similar to the existence of \REQUIRE and \ENSURE.

If you want to write this outside of the algorithm float, then you only need to use the algorithmic environment content. If you wish to also add a caption, then I suggest you use \begin{algorithm}[H] ...\end{algorithm} instead (algorithm uses float to define the algorithm environment, and therefore supports the "HERE" float specification).

Related Question