Yes, this is one of the ever-tricky cases of "did you mean the container or
the elements of that container?" Setindex always assumes that, if the RHS
of the assignment is an array of some sort, it's the elements that should
be assigned.
David, `A[:] = Vector[[1,2,3]]` works as you'd expect (this is the array
concatenation vs construction issue), but this will only work if
`length(A)==1`.
And Linus, an easy work-around (assuming that you don't always just have a
one-element) is to use `fill!` instead: `fill!(A, [1,2,3])`.
On Tuesday, July 28, 2015 at 8:45:44 AM UTC-4, David Gold wrote:
>
> That error makes sense to me, insofar as `A[:]` is not a scalar entry but
> a whole vector. So assigning the vector `[1, 2, 3]` to `A[:]` is
> interpreted as, "put all the things from this vector into that vector". But
> by that logic, `A[:] = [[1, 2, 3],]` ought to work, and it doesn't. Of
> course, scalar assignment works as expected.
>
> If you can't avoid colon, one thing you can do is define a wrapper type
> that isn't interpreted as container of objects each of which is to be
> assigned to an index in `A[:]`, as well as an appropriate convert method:
>
> julia> type ArrayWrapper{T, N}
> A::Array{T, N}
> end
>
> julia> Base.convert{T, N}(::Type{Array{T, N}}, X::ArrayWrapper{T, N}) = X.A
> convert (generic function with 700 methods)
>
> julia> A[:] = ArrayWrapper([1, 2, 3])
> ArrayWrapper{Int64,1}([1,2,3])
>
> julia> A
> 1-element Array{Array{Int64,1},1}:
> [1,2,3]
>
>
> On Tuesday, July 28, 2015 at 4:06:59 AM UTC-4, Linus Härenstam-Nielsen
> wrote:
>>
>> I'm implementing a setindex! method for an object with a field of type
>> Matrix{Vector{Float64}} (a matrix of vectors) and I've run into a problem
>> with getting the Colon() index to work properly. It comes down to this:
>>
>> julia> A = Array(Vector{Int}, 1)
>> 1-element Array{Array{Int64,1},1}:
>> #undef
>>
>> julia> A[:] = [1,2,3]
>> ERROR: DimensionMismatch("tried to assign 3 elements to 1 destinations")
>> in throw_setindex_mismatch at operators.jl:226
>>
>> Since the element I'm trying to assign is a vector setindex! calls the
>> wrong method for my purposes. What I would like is for the resulting array
>> to be:
>>
>> julia> A
>> 1-element Array{Array{Int64,1},1}:
>> [1,2,3]
>>
>> Is there a way to get around this? I would change the datatype to
>> Array{Float64, 3} if I could but other functions rely on the current
>> structure.
>>
>