On Fri, 15 Dec 2017, Allin Cottrell wrote:
On Fri, 15 Dec 2017, Schaff, Frederik wrote:
> Hi there, I'd expect both options to be equivalent (add a matrix with the
> data from the series to the array of matrices). However, the forach loop
> only adds the first data-point to the matrix.
>
> <hansl>
> open denmark.gdt -q
> list ls = 1 4
> matrices M = null
> loop foreach l ls
> matrix temp = l
> colnames(temp,varname(l))
> M+=temp
> endloop
>
> matrices M2 = null
> loop s=1..nelem(ls)
> matrix temp = ls[s]
> colnames(temp,varname(ls[s]))
> M2 += temp
> endloop
In your first loop, 'l' is a just scalar index, so
matrix temp = l
gives you {1} on the first iteration and {2} on the second. If you want to
access the series at position l in the list you need to say
matrix temp = $l
Then you'll get the same output as from your second loop.
P.S. the varname() call in your first loop also needs fixing: on the
second iteration l = 2 but presumably you want the name of the second
series in the list, namely series 4, so:
colnames(temp,varname(ls[l]))
Allin