Is it possible to store an array generating by foreach?

Here is a way.

\documentclass{article}
\usepackage{tikz}

\makeatletter
\newcommand\store[6][\x]{%
  % #1 = dummy variable
  % #2 = variable to store the list
  % #3 = expression
  % #4 = expression to store (with \pgfmathresult) and possibly #1
  % #5 = start point
  % #6 = end point
  \gdef\store@temp{\@gobble}%
  \foreach #1 in {#5,...,#6}{\pgfmathparse{#3}\xdef\store@temp{\store@temp,#4}}%
  \let#2=\store@temp
}
\makeatother

\begin{document}

\store{\sineslist}{sin(deg(\x))}{\pgfmathresult}{1}{100}

\show\sineslist

\store[\i]{\randlist}{rand}{(\i,\pgfmathresult)}{1}{20}

\show\randlist

\end{document}

Here's what's reported:

> \sineslist=macro:
->0.84143,0.90924,0.14111,-0.75677,-0.95886,-0.27939,0.65697,0.9893,0.41208,-0.
544,-0.99995,-0.53654,0.42015,0.99057,0.65025,-0.28787,-0.96138,-0.75095,0.1498
6,0.91292,0.83662,-0.00885,-0.84619,-0.90555,-0.13234,0.76251,0.95636,0.27087,-
0.6636,-0.988,-0.40402,0.55139,0.99988,0.52907,-0.42815,-0.99173,-0.64352,0.296
34,0.96375,0.74509,-0.1586,-0.91647,-0.83174,0.01768,0.85086,0.90175,0.12357,-0
.7682,-0.9537,-0.26234,0.67021,0.98657,0.3959,-0.55878,-0.99971,-0.52153,0.4361
4,0.99284,0.6367,-0.30478,-0.9661,-0.73914,0.16733,0.92,0.8268,-0.02654,-0.8554
7,-0.8979,-0.11476,0.77385,0.95105,0.2538,-0.67673,-0.98512,-0.38777,0.56606,0.
99948,0.51398,-0.44408,-0.99385,-0.62988,0.3132,0.9683,0.73317,-0.17606,-0.9234
,-0.82178,0.03539,0.86002,0.89395,0.10597,-0.77942,-0.94824,-0.24522,0.68324,0.
98354,0.3796,-0.57335,-0.99916,-0.50635.

> \randlist=macro:
->(1,-0.86513),(2,-0.8214),(3,0.83136),(4,0.03825),(5,0.0602),(6,0.65846),(7,-0
.7918),(8,0.8938),(9,-0.13474),(10,-0.90755),(11,0.68077),(12,-0.1404),(13,-0.4
3602),(14,-0.08235),(15,0.65369),(16,-0.25284),(17,0.98817),(18,-0.09077),(19,0
.42735),(20,0.88712).

I have to say that I didn't get the distance argument in your question. I would not go with PGF or TikZ but produce data arrays stored in a file. That way you can access to the pgfplotstable and its quite fast macros. The TikZ part chokes in about 150 but the latter example goes pretty far (one other reason of course my code is not optimized at all so it's not only TikZ' fault).

Here is an example with 100 sample points drawn just with storing into arrays and reading them later and also a PGFPLOTSTABLE example I have compiled from the manual + some tweaks.

\documentclass{article}
\usepackage{pgfplotstable,booktabs}

%\pgfmathsetseed{226584}

\def\sample{100}
\pgfmathsetmacro{\runningrandarray}{rand} % Initial
\edef\runningmean{\runningrandarray}

\foreach \x[count=\xi from 1] in {2,...,\sample}{
\let\temprand\runningrandarray
\pgfmathsetmacro\tempres{rand}
\xdef\runningrandarray{\temprand,\tempres}
\pgfmathparse{(\xi*\runningmean+\tempres)/\x}
\xdef\runningmean{\pgfmathresult}
}
\xdef\randarray{{\runningrandarray}} % Double brace needed if you want to access via TikZ

\begin{document}
Some random points here : 

\begin{tikzpicture}
\foreach \x in {0,...,\number\numexpr\sample-1\relax}{
\pgfmathsetmacro\ycoord{\randarray[\x]}
    \node[circle,inner sep=1pt,fill=red,ultra thin] at (\x mm,\ycoord mm){};
    }
\end{tikzpicture}

and their current mean value is \runningmean

Instead we can directly go with PGFPLOTSTABLE package to produce the values: 

\pgfplotstablenew[
create on use/y/.style={create col/expr={\pgfplotstablerow+1}},
% define how the 'sine' column shall be filled:
create on use/sine/.style={create col/expr={sin(\pgfplotstablerow+1)}},
columns={y,sine}]{130}\loadedtable

\vspace{1cm}
\begin{minipage}[t]{0.58\textwidth}
show it as a table (columns skipped):
\centering
\pgfplotstabletypeset[
every head row/.style={before row=\toprule,after row=\midrule},
every last row/.style={after row=\bottomrule},
row predicate/.code={%
\ifnum#1>5\relax
\ifnum#1<125\relax
\pgfplotstableuserowfalse
\fi
\fi}
]
\loadedtable
\end{minipage}
\begin{minipage}[t]{0.4\textwidth}
or directly use it in plots

\begin{tikzpicture}
    \begin{axis}[width=4cm,height=7cm,xlabel={angle [deg]}]
    \addplot[no marks] table[x=y,y=sine]\loadedtable;
    \end{axis}
\end{tikzpicture}
\end{minipage}
\end{document}

enter image description here

Tags:

Tikz Pgf