Quantcast
Channel: First steps - JuliaLang
Viewing all articles
Browse latest Browse all 2795

How to change arrays of a mutable struct

$
0
0

Hey there, I am currently trying to make a Dataset struct object that has new X and y values to it appended to it over time. This is my set up

mutable struct Dataset{T<:Real}
    X::AbstractMatrix{T}
    y::AbstractVector{T}
end


function Dataset(
        X::AbstractMatrix{T},
        y::AbstractVector{T}
	) where {T<:Real}
    return Dataset{T}(X, y)

end

function extendDataset(dataset::Dataset, X::AbstractMatrix{T}, y::AbstractVector{T}) where {T <: Real}
	catX = hcat(dataset.X, X)
	caty = vcat(dataset.y, y)
    dataset.X = catX
    dataset.y = caty
    dataset.X = 2 * dataset.X
    dataset.y = 2 * dataset.y
end

All 4 of these assignments, even the ones that simply do a 2 * dataset.X and 2 * dataset.y give me the following error whenever I call extendDataset:

MethodError: convert(::Type{Union{}}, ::Array{Float32,1}) is ambiguous. Candidates:
  convert(::Type{Union{}}, x) in Base at essentials.jl:169
  convert(::Type{T}, a::AbstractArray) where T<:Array in Base at array.jl:554
  convert(::Type{T}, arg) where T<:VecElement in Base at baseext.jl:8
  convert(T::Type{var"#s91"} where var"#s91"<:BitArray, a::AbstractArray) in Base at bitarray.jl:574
  convert(T::Type{var"#s828"} where var"#s828"<:SparseArrays.SparseVector, m::AbstractArray{T,1} where T) in SparseArrays at /home/dhananjay/julia-1.5.3/share/julia/stdlib/v1.5/SparseArrays/src/sparsevector.jl:434
  convert(T::Type{var"#s828"} where var"#s828"<:SharedArrays.SharedArray, a::Array) in SharedArrays at /home/dhananjay/julia-1.5.3/share/julia/stdlib/v1.5/SharedArrays/src/SharedArrays.jl:369
  convert(::Type{SA}, a::AbstractArray) where SA<:(StaticArrays.SArray{Tuple{},T,0,1} where T) in StaticArrays at /home/dhananjay/.julia/packages/StaticArrays/NTbHj/src/Scalar.jl:12
Possible fix, define
  convert(::Type{Union{}}, ::Array{T,1} where T)
convert at ./some.jl:34

Does anyone know what I am doing wrong?

3 posts - 2 participants

Read full topic


Viewing all articles
Browse latest Browse all 2795

Trending Articles