julia lang - storing several matrices on one object -


i have simple econometrics/statistics exercise i'm trying implement in julia. i've never used julia before, , have done code in r, cant seem translate code.

here outline: have inner loop fixed n, draw k samples distribution, , store them in matrix n x k. 2 matrices, them multiply each column of each matrix (obtaining 2x1 vector) them , store in 2 x k matrix.

after that, outer loop for, say, 5 different values of n. in end, have 5 different matrices 2 x k, can plot them. can't figure out how store efficiently matrix. in r, put them in list, , call them each out calculations.

using distributions using statsbase j = 500 n = [10 100 500 1000 10000] = 1 b = ones(3,j) n in n     x = ones(n,j)     e = ones(n,j)     y = ones(n,j)     j = 1:j         x[:,j] = rand(normal(3,1),n)         y[:,j] = 3 + 2.5*x[:,j] + e[:,j]         x = [y[:,j] x[:,j]]         b[:,j] = inv(y'*x)*(x'*y[:,j])     end     = + 1     end 

i've tried code, doesn't seem work @ all. can't make simple loop below work:

for = 1:10     x = ones(i, 10) end 

i error: undefvarerror: x not defined. can guys me?

you'd put them in vector{matrix}, vector of matrices. common paradigm in julia instead define function fthat want, , broadcast function on n, i.e. calling a = f.(n) (n should vector, not matrix, btw, commas between numbers rather spaces).

so, want store 5 matrices 2 x k varying k. there in fact numerous ways can that, depending on taste, style, convenience, etc. here examples, k varies 20 24:

# python-style list comprehension mymatrices = [randn(k,2) k in 20:24]  # function broadcast on array f(k) = randn(k,2) mymatrices = f.(20:24)  # vector preallocated elements mymatrices = vector{matrix{float64}}(5) (i, k) in enumerate(20:24)   #enumerate smart     mymatrices[i] = randn(k, 2) end      # vector grows dynamically mymatrices = matrix{float64}[] # shorter optional syntax empty vector (i, k) in enumerate(20:24)     push!(mymatrices, randn(k, 2)) end    # map anonymous function mymatrices = map(k -> randn(k,2), 20:24) 

Comments

Popular posts from this blog

PHP and MySQL WP -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

go - golang pprof for c library code -