Yes, this was the problem. I now have results that match the output from R. 
I've updated the gist with your line for the correct way to create a vector 
of vectors. I will have to spend some more time to understand the 
make-vector behavior. Perhaps my thinking is too constrained by my R 
experience where many functions are "vectorized"? I guess the part that was 
confusing me (and still is) is why make-vector worked as expected (by me) 
for my inner vector but not my outer vector. Thanks!


On Sunday, February 10, 2019 at 12:42:53 AM UTC-8, Alex Harsanyi wrote:
>
> This line looks suspicious:
>
>      (define results (make-vector years (make-vector (vector-length 
> fecundity) 0)))
>
> The "(make-vector (vector-length fecundity) 0)" expression will create a 
> single vector, than it creates the outer vector will all elements pointing 
> to it.  It is not a matrix, but a "column" vector where each element is 
> referencing the same row vector.  This means that if you update an element 
> in one of the rows, the same value will "appear" in all other rows. The 
> only row that is different is the first one which you initialize in the 
> line:
>
>     (vector-set! results 0 (make-vector (vector-length fecundity) 10))
>
> What you probably want is a vector of vectors, which can be built like this
>
>     (define results (for/vector ([index (in-range years)]) (make-vector 
> (vector-length fecundity) 0)))
>
> Alex.
>
>
>
> On Sunday, February 10, 2019 at 3:40:42 PM UTC+8, travis.h...@gmail.com 
> wrote:
>>
>> Hi All,
>>
>> I'm an R programmer that has recently started learning Racket. I decided 
>> to start by trying to create a simple age-structured population model. In 
>> R, I would initialize a matrix and use nested for loops to move through the 
>> elements of the matrix and propagate the population forward through time. 
>> For my first attempt in Racket (
>> https://gist.github.com/hinkelman/3ee6115cdd7f0a4c8f1672b7d8df5c27), I 
>> used for* to loop through a vector of vectors. The code in that gist 
>> doesn't quite work. There is apparently something wrong with how I'm using 
>> vector-set! such that "rows" in my vector of vectors are being updated 
>> prematurely. I would greatly appreciate it if someone could point out what 
>> I'm doing wrong there. I'm also interested in suggestions for alternative 
>> approaches because it seems unlikely that I have written this code in 
>> idiomatic Racket.
>>
>> Thanks,
>>
>> Travis
>>
>> P.S. If it is helpful, here is a gist (
>> https://gist.github.com/hinkelman/d5b8414b0c6383057d7846509a724bbf) with 
>> the R code that I was trying to write in Racket.
>>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to