我想在论文中以表格形式写一个算法。我尝试过写,但我想在这个算法中写“While”而不是“for”。我该怎么做?我想在文本的任何地方都放上表格。
\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}
答案1
该环境中还有更多工具可供您使用algorithmic
:
\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
(从algorithmicx
) 提供\WHILE{<condition>}
和\ENDWHILE
。如您所见,它还缩进语句以显示其(嵌套)结构。
为了完整起见,我还定义了\INPUT
和,类似于和\OUTPUT
的存在。\REQUIRE
\ENSURE
如果你想写这个外部浮动algorithm
,则只需使用algorithmic
环境内容。如果您还想添加标题,那么我建议您使用\begin{algorithm}[H]
... \end{algorithm}
(algorithm
用途float
定义algorithm
环境,因此支持“HERE”浮动规范)。