Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Collection of ranges in Julia

Tags:

julia

The task appears simple, but i am struggling to get the way of initializing a vector (array, collection, or similar) of ranges. That is, I need to do something like this:

vec_of_ranges = HOW TO INITIALIZE THIS?
for i=1:10
   range = i:20
   vec_of_ranges[i]=range
end

Can anyone give me a hint on how to do this? I need it so I can then evaluate a given array on that collection of ranges...

Thanks in advance!

like image 423
GEBRU Avatar asked Jul 07 '20 14:07

GEBRU


People also ask

How to define range in Julia?

For example if start = 1,stop = 100 and length = 10, then we will have range as 1.0:11.0:100.0 Where 11.0 is difference between two subsequent objects in the range. For this example, we will have a range of objects as 1.0, 12.0, 23.0, ... 100.0 .

How do you create an array in Julia?

A 1D array can be created by simply writing array elements within square brackets separated by commas(, ) or semicolon(;). A 2D array can be created by writing a set of elements without commas and then separating that set with another set of values by a semicolon.

How do you define a vector in Julia?

A Vector in Julia can be created with the use of a pre-defined keyword Vector() or by simply writing Vector elements within square brackets([]). There are different ways of creating Vector. vector_name = [value1, value2, value3,..] or vector_name = Vector{Datatype}([value1, value2, value3,..])


1 Answers

If you type typeof(1:50) you get UnitRange{Int64}

So you need an array of UnitRange

Try this:

vec_of_ranges = Array{UnitRange{Int64},1}(undef, 10)
for i=1:10
   range = i:20
   vec_of_ranges[i]=range
end

You might also want to use the fill() command

vec_of_ranges = fill(1:20, 10)
like image 91
JAlex Avatar answered Sep 30 '22 09:09

JAlex