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}
Best Answer
There are more tools at your disposal inside the
algorithmic
environment:algcompatible
(fromalgorithmicx
) 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 thealgorithmic
environment content. If you wish to also add a caption, then I suggest you use\begin{algorithm}[H]
...\end{algorithm}
instead (algorithm
usesfloat
to define thealgorithm
environment, and therefore supports the "HERE" float specification).