[Tex/LaTex] Write numbers and draw rectangles with foreach

tikz-pgf

enter image description here

I am trying to draw a below given diagram for myself. I have been able to draw the rectangles using \foreach however I am unable to write numbers below the boxes as shown. Could somebody help me in drawing such a diagram.

Here is my code, it is IEEE Tran's code. Also, please see that I have manually added numbers from 1 to n, I want to do it using \foreach command. Kindly do not give negative voting because I am beginner with Tikz and this forum.

Please answer one more question- is there a way I can initialize x and y coordinates as x = 1 and y = 1 in the starting of the Tikz figure and then draw all the blocks/rectangles using reference coordinates?

\documentclass[conference]{IEEEtran}

\ifCLASSINFOpdf

\else
\fi
\usepackage[usenames,dvipsnames]{color}
\usepackage[T1]{fontenc}
\usepackage{tikz}
\usetikzlibrary{positioning}
\usetikzlibrary{arrows}
\usetikzlibrary{shapes.multipart}
\usepackage{subfig}
\usepackage{tabularx,tikz}


\hyphenation{op-tical net-works semi-conduc-tor}


\begin{document}

\title{Bare Demo of IEEEtran.cls for Conferences}

\maketitle

\IEEEpeerreviewmaketitle

\section{Introduction}
I wish you the best of success.

\begin{tikzpicture}
%Can I give a reference coordinate such as x = 0 and y = 0 and then draw figures
%based on that reference points through out the picture?

\draw [rounded corners](0,0) rectangle +(9,6);

\draw [rounded corners](0.15,0.5) node[above right, font =\small,align=center,text width = 2cm]{YYYY} rectangle +(2.25,5);

\foreach \x in {2.5}
    \foreach \y in {0.5,3,4.5}
    {
    \draw [rounded corners] (\x,\y) rectangle +(6.35,1);

    }

\foreach \x in {2.75,4.25,5.75,7.25}
    \foreach \y in {0.9,3.4,4.9}
    {
    \draw [rounded corners] (\x, \y) node[above right, font=\scriptsize,text width = 1.8cm] {XXX} rectangle +(1.2,0.5);
    }
\node at (2.75,4.5) [above right, font=\scriptsize,text width = 1.8cm] {1} ;
\node at (2.75,3) [above right, font=\scriptsize,text width = 1.8cm] {2} ;
\node at (2.75,0.5) [above right, font=\scriptsize,text width = 1.8cm] {n} ;

\node at (4.25,4.5) [above right, font=\scriptsize,text width = 1.8cm] {n+1} ;
\node at (4.25,3) [above right, font=\scriptsize,text width = 1.8cm] {n+2} ;
\node at (4.25,0.5) [above right, font=\scriptsize,text width = 1.8cm] {2n} ;

\node at (5.75,4.5) [above right, font=\scriptsize,text width = 1.8cm] {2n+1} ;
\node at (5.75,3) [above right, font=\scriptsize,text width = 1.8cm] {2n+2} ;
\node at (5.75,0.5) [above right, font=\scriptsize,text width = 1.8cm] {3n} ;

\node at (7.25,4.5) [above right, font=\scriptsize,text width = 1.8cm] {3n+1} ;
\node at (7.25,3) [above right, font=\scriptsize,text width = 1.8cm] {3n+2} ;
\node at (7.25,0.5) [above right, font=\scriptsize,text width = 1.8cm] {4n} ;

\end{tikzpicture}

\end{document}

Best Answer

Here is an example using two nested \foreach loops and \ifthenelse from the ifthen package:

\documentclass[tikz,border=1cm]{standalone}

\usepackage{ifthen}

\begin{document}

\begin{tikzpicture}[
    rect/.style={rectangle,rounded corners,fill=green,draw=black,text width=.5cm,text height=.25cm},
    none/.style={rectangle,rounded corners,fill=green!10,draw=gray!20,text width=.5cm,text height=.25cm},
    nmbr/.style={font=\scriptsize,yshift=-.25cm,anchor=north}
]

\newcounter{n}

\foreach \y in {1,...,3} {
  \setcounter{n}{0}
  \foreach \x in {1,...,4} {
    \ifthenelse{\y<3}{
      \node[rect] at (\x,-\y) {};
      \node[nmbr] at (\x,-\y) {\ifthenelse{\x=1}{\y} {
        \ifthenelse{\arabic{n}=1}{}{\arabic{n}}n+\y}};
      \stepcounter{n}
    }{
      \stepcounter{n}
      \node[none] at (\x,-\y) {};
      \node[rect] at (\x,-\y-1) {};
      \node[nmbr] at (\x,-\y-1) {\ifthenelse{\x=1}{n}{\arabic{n}n}}; 
    }
  }
}

\end{tikzpicture}

\end{document} 

enter image description here