What's the proper way to do assignment for struct array in Julia? -
i have issue assignment struct array (or constructor struct array) in julia. suppose want below tasks:
# define struct struct grn_net gene_net::matrix{float64} init_s::vector{float64} end # define empty struct array net = array{grn_net}(100) # initialize struct array n in 1:100 net[n] =grn_net(rand(5,5),rand(-1.:2.:1.,5)) end # make copy of net new_net = deepcopy(net) # initialize temporary matrix temp_mat = zeros(float64,5,5) n in 1:100 # gernerate 2 unique random positions rand_pos = randperm(100)[1:2] # randomly swap rows selected matrices index = rand(0:1,gene_n) m in 1:5 if index[m]==1 temp_mat[m,:] = net[rand_pos[1]].gene_net[m,:] else temp_mat[m,:] = net[rand_pos[2]].gene_net[m,:] end end # save result matrix new net new_net[n] = grn_net(temp_mat,rand(-1.:2.:1.,5)) end
so, if run code, find new_net
have same gene_net
entries in struct array. when debug code, found, although temp_mat
changed @ each step, new_net[n-1].gene_net
reset save same matrix in new_net[n].gene_net
. so, think problem new_net[n].gene_net
sharing same space temp_mat
. so, changed new_net[n] = grn_net(temp_mat,rand(-1.:2.:1.,5))
new_net[n] = grn_net(deepcopy(temp_mat),rand(-1.:2.:1.,5))
, , problem fixed. however, i'm not sure how new_net
, temp_mat
sharing same space? or what's problem here? clarify issue?
btw, i'm not sure if title appropriate one, if can think of better one, please revise it.
Comments
Post a Comment