user3582228
user3582228

Reputation: 181

how to convert an array{Float64,1} to a Float64? in julia

I just want to know how to convert an array{Float64,1} to a Float64? in julia

utility= rand(1)*row*c*(1-x)

it gives me error "array of size (1,) passed as objective; only scalar objectives are allowed"

Upvotes: 0

Views: 628

Answers (1)

mbauman
mbauman

Reputation: 31342

Just use rand() instead of rand(1). The former returns a random Float64 value, whereas the latter returns a 1-dimensional array with one element in it. Better to keep everything as scalars in the first place if it's possible to do so.

Generally though, you can't convert a vector v to a scalar. There may be more than one element in it, in which case the conversion isn't well defined. What you can do, however, is index into the vector to extract one of its values.

Upvotes: 3

Related Questions