Just do x[i] = rand(2, 2) (without the dot) instead of x[i] .= rand(2, 2). That way you are making the element x[i] point to the new matrix that is created by rand(2, 2). By using broadcasting, you try to overwrite the values in the matrix x[i], but it fails because the matrix x[i] does not exist.
Maybe I am misunderstanding you, but why do you think that specifying the element type would change things here? Since the original element type is already Matrix{Float64}, specifying it again doesn’t change anything.
What I had in mind was to apply this function to a vector like x:
function claytonsample!(matkk, τ; randmat=randmat)
matkk .= randmat
τ == 0 && return matkk
n = size(matkk, 1)
for i in 1:n
v = matkk[i, 2]
u = matkk[i, 1]
matkk[i, 2] = (1 - u^(-τ) + u^(-τ)*v^(-(τ/(1 + τ))))^(-1/τ)
end
return matkk
end
randmat = rand(10, 2)
matkk = similar(randmat)
#this works:
claytonsample!(matkk, 0.3; randmat=randmat)
#this doesn't:
randmat2 = [rand(10, 2) for i in 1:3]
matkk2 = similar(randmat2)
for i in 1:3
claytonsample!(matkk2[i], 0.3; randmat=randmat2[i])
end