0

In a two-dimensional array I want to store some calculated values. However, for example for a 2 on 2 matrix I get identical values for [1,2] and [2,1]. What am I am doing wrong?

\documentclass[11pt]{scrartcl}
\usepackage[utf8x]{inputenc}
\usepackage{fp}                                 % fp-package
\usepackage{forloop,arrayjobx}

\begin{document}
\newcommand{\CreateArray}[2]{
\FPeval\maxi{1+#1} \FPeval\maxj{1+#2}
\FPclip\maxi{\maxi} \FPclip\maxj{\maxj} 
\newcounter{i} \newcounter{j}
\newarray\MatrixA
\expandarrayelementtrue

When I set up the matrix everything works fine:

\forloop{i}{1}{\thei < \maxi}{                  % Rows
    \forloop{j}{1}{\thej < \maxj}{              % Columns
        \FPeval\calcvar{\thej+100*\thei}        % use variables i and j for calculation
        \FPclip\calcvar{\calcvar}
        \MatrixA(\thei,\thej)={\calcvar}        % save \calcvar value in matrix     
        \FPprint{\thei} - \FPprint{\thej} - \FPprint{\calcvar} \\
    }
}

But once I want to se the values stored the fields [2,1] and [1,2] are identical:

\forloop{i}{1}{\thei < \maxi}{                  % Rows
    \forloop{j}{1}{\thej < \maxj}{              % Columns
        \thei - \thej - \MatrixA(\thei,\thej) \\        
    }
}
}
\CreateArray{2}{2}                                  % crete matrix
\end{document}
1

I have found a workaround but still I am interested in your suggestions for a solution of the initial problem. I found out that lists are working just fine, so I decided to use a long list while for every "i" I just go 1000 entries higher and start save my entries at "i*1000+j". This may work even for multidimensional arrays depending on the maximum length one list may have. Still this is not the solution I was looking for and hope that you can help me to get better code.

\documentclass[11pt]{scrartcl}

\usepackage[utf8x]{inputenc}
\usepackage{fp}                                 % fp-package
\usepackage{forloop,arrayjobx}


\begin{document}

\newcommand{\CreateArray}[2]{
    \FPset\jump{1000}                               % array as long list, max \jump values per column
    \FPeval\maxi{1+#1} \FPclip\maxi{\maxi} 
    \FPeval\maxj{1+#2} \FPclip\maxj{\maxj} 
    \newcounter{i} \newcounter{j} \newarray\MatrixA
    \expandarrayelementtrue
    \forloop{i}{1}{\thei < \maxi}{                  % Rows
        \forloop{j}{1}{\thej < \maxj}{              % Columns
            \FPeval\iloc{1+\thei*\jump-\jump} \FPclip\iloc{\iloc}
            \FPeval\calcvar{\thej+100*\thei}        
            \FPclip\calcvar{\calcvar}
            \MatrixA(\iloc,\thej)={\calcvar}        % save rsults in MatrixA Matrix     
            %           \FPprint{\thei} - \FPprint{\thej} -      
\FPprint{\calcvar} \\ % Testausgabe
        }
    }
    \forloop{i}{1}{\thei < \maxi}{                  % Rows
        \forloop{j}{1}{\thej < \maxj}{              % Columns
            \FPeval\iloc{1+\thei*\jump-\jump} \FPclip\iloc{\iloc}
            \thei - \thej - \MatrixA(\iloc,\thej) \\        
        }
    }
}
\CreateArray{3}{3}                                  % Matrix with {rows}{columns} 

\MatrixA(1,1)       \MatrixA(1,2)   \MatrixA(1,3)       \\
\MatrixA(1001,1)    \MatrixA(1001,2) \MatrixA(1001,3)   \\
\MatrixA(2001,1)    \MatrixA(2001,2) \MatrixA(2001,3)

\begin{tabular}{ccc}
    \forloop{i}{1}{\thei < \maxi}{                  % Rows
        \forloop{j}{1}{\thej < \maxj}{              % Columns
            \FPeval\iloc{1+\thei*\jump-\jump} \FPclip\iloc{\iloc}
            \MatrixA(\iloc,\thej)       
        }
        \\
    }
\end{tabular}
\end{document}
0

You're missing \dataheight=2 (corresponding to \maxj), which needs to be inserted prior to the nested forloops that perform the initial assignments.

To be clear, modify the following part by adding \dataheight=2

When I set up the matrix everything works fine:
\dataheight=2
\forloop{i}{1}{\thei < \maxi}{                  % Rows
    \forloop{j}{1}{\thej < \maxj}{              % Columns
        \FPeval\calcvar{\thej+100*\thei}        % use variables i and j for calculation
        \FPclip\calcvar{\calcvar}
        \MatrixA(\thei,\thej)={\calcvar}        % save \calcvar value in matrix     
        \FPprint{\thei} - \FPprint{\thej} - \FPprint{\calcvar} \\
    }
}

I had a very similar problem ("Why does arrayjobx return the wrong elements of the bi-dimensional matrix I defined?") and egreg solved it with the same advice I'm giving you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.