Gregory
Gregory

Reputation: 349

Creating an array of arrays from elements

I have a question regarding building a bunch of arrays off of a single array.

Suppose I have myarr = [2,3]. I want to build an array of arrays that contains elements that are at least half as big (going up to some limit I have defined maxSize as a single element, but leaves the others unchanged) and with steps defined (e.g. stepsize=0.5). For example suppose maxsize = 5, the first element in myarr is 2 and so I want to create:

[1,3]
[1.5,3]
[2,3] # Ideally I should exclude myarr...
[2,1.5]
[2,2]
[2,2.5]
[2,3]

Is there anyway to construct this?

Upvotes: 1

Views: 52

Answers (1)

Benoit Pasquier
Benoit Pasquier

Reputation: 3005

It seems you want

julia> [(x = Float64.(myarr); x[idx] = v; x) for idx in 1:length(myarr) for v in myarr[idx]/2:stepsize:min(maxsize, myarr[idx]) if v ≠ myarr[idx]]
5-element Array{Array{Float64,1},1}:
 [1.0, 3.0]
 [1.5, 3.0]
 [2.0, 1.5]
 [2.0, 2.0]
 [2.0, 2.5]

Upvotes: 2

Related Questions