Arrigo
Arrigo

Reputation: 259

Creating a 1x1 Julia array

I would like to create a 1×1 array (say an Array{Float64,2}) and initialize it to some value. Of course this works:

M=zeros(1,1)
M[1,1]=0.1234

Is there a more concise way to create M and initialize it at the same time?

Upvotes: 3

Views: 438

Answers (3)

Fengyang Wang
Fengyang Wang

Reputation: 12061

The existing answers are not what I would recommend. The best way is to use

julia> hcat(5)
1×1 Array{Int64,2}:
 5

This is most concise and parallels the [x y] concatenation form.

Upvotes: 1

Andy Hayden
Andy Hayden

Reputation: 375925

An alternative is to reshape:

julia> reshape([1.234], 1, 1)
1x1 Array{Float64,2}:
 1.234

Upvotes: 1

vchuravy
vchuravy

Reputation: 1238

Since [1.1234] will give you a Vector in Julia the simplest way I could come up with is:

julia> fill(1.234,1,1)
1x1 Array{Float64,2}:
 1.234

Upvotes: 3

Related Questions