[Tex/LaTex] Undefined Control Sequence \For, \state, or \EndFor

algorithmicloopspseudocode

I want to write this algorithm but the error Undefined Control Sequence \For, \state, or \EndFor pops up.

\documentclass[titlepage,oneside,12pt]{article}
\usepackage[]{algorithm2e,algpseudocode}
\begin{document}
    \begin{algorithmic}[H]
     \KwData{Given the M detected objects}
     \KwResult{new clusters}
     \For (each object $i$)
        \state Create new cluster $C_i$;
        \state Initialize the cluster feature;
        \state $cluster Feature(i) = (x_i, y_i, v_{xi}, v_{yi}, 1, 0, i)$;
     \EndFor
     \caption{Initialization}
    \end{algorithmic}
\end{document}

I saw related questions but the case wasn't as mine.

Undefined control sequence \State \COMMENT

LaTeX algorithmic package: Undefined Control Sequence in for-Loop

Best Answer

You are mixing up commands from the packages algorithm2e and algorithm. Well, I believe you do.

See my following MWE. This does compile and uses the syntax provided by the documentation here.

Is this what you wanted to type?

% arara: pdflatex

\documentclass[titlepage,oneside,12pt]{article}
\usepackage{algpseudocode,algorithm2e}
\begin{document}
    \begin{algorithm}[H]
     \KwData{Given the M detected objects}
     \KwResult{new clusters}
     \For{each object $i$}{
        Create new cluster $C_i$\;
        Initialize the cluster feature\;
        $\mathit{cluster Feature}(i) = (x_i, y_i, v_{xi}, v_{yi}, 1, 0, i)$\;
                }
     \caption{Initialization}
    \end{algorithm}
\end{document}

enter image description here