A = rand(3,3)
b = findall(vec(A).>0.5) # contains indices
c = rand(2,length(b)) # some values that we want to have at the corresponding indexes stored in b, in array B.
B = rand(2,3,3)
B[:,b] = c # this line returns an error
ERROR: BoundsError: attempt to access 2Γ3Γ3 Array{Float64, 3} at index [1:2, [1, 4, 5, 7, 8, 9]]
Stacktrace:
[1] throw_boundserror(A::Array{Float64, 3}, I::Tuple{Base.Slice{Base.OneTo{Int64}}, Vector{Int64}})
@ Base ./abstractarray.jl:651
[2] checkbounds
@ ./abstractarray.jl:616 [inlined]
[3] _setindex!
@ ./multidimensional.jl:886 [inlined]
[4] setindex!(::Array{Float64, 3}, ::Matrix{Float64}, ::Function, ::Vector{Int64})
@ Base ./abstractarray.jl:1267
[5] top-level scope
@ REPL[15]:1
In the last line, I would like that the value in c fill B in its 3x3 part (second and third dimension of B) at the locations contained in b for a 3x3 matrix (locations obtained from A). How should I write the last line ?
A = rand(3,3)
b = findall(vec(A).>0.5) # contains indices
c = rand(2,length(b)) # some values that we want to have at the corresponding indexes stored in b, in array B.
B = rand(2,3,3)
b = CartesianIndices(A)[b] # ADDING THIS LINE SOLVES THE PROBLEM
B[:,b] = c # b as CartesianIndices is legal here
Vector b as CartesianIndices is a legal input in this scenario (instead of βLinearIndicesβ).
@jling Thank you for your suggestion with the loop, but I will avoid using a loop if there is an alternative.