Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

End of array in variable in julia

Tags:

arrays

julia

I would like to slice an array a in Julia in a loop in such a way that it's divided in chunks of n samples. The length of the array nsamples is not a multiple of n, so the last stride would be shorter.

My attempt would be using a ternary operator to check if the size of the stride is greater than the length of the array:

for i in 0:n:nsamples-1
    end_ = i+n < nsamples ? i+n : end
    window = a[i+1:end_]
end

In this way, a[i+1:end_] would resolve to a[i+1:end] if I'm exceeding the size of the array.

However, the use of the keyword "end" in line 2 is not acceptable (it's also the keyword for "end of control statement" in julia.

In python, I can assign None to end_ and this would resolve to a[i+1:None], which will be the end of the array.

How can I get around this?

like image 754
gozzilli Avatar asked Dec 04 '15 10:12

gozzilli


People also ask

How do you access array elements in Julia?

Elements of an array can be easily extracted with the use of square brackets([]). Loops can also be used to extract more than one element from an array. Also, a range of elements can also be extracted from an array by passing a range within square brackets just like Tuples.

Why do Julia arrays start at 1?

Arrays in the Julia programming language are somewhat different from arrays in other programming languages. Not because of different behavior, but because they are start at 1 instead of 0. It's because Julia is used for mathematics , machine learning and scientific etc.

How do you make an array of zeros in Julia?

This is usually called with the syntax Type[] . Element values can be specified using Type[a,b,c,...] . zeros([T=Float64,] dims::Tuple) zeros([T=Float64,] dims...) Create an Array , with element type T , of all zeros with size specified by dims .

How do you fill an array in Julia?

Create an array filled with the value x . For example, fill(1.0, (10,10)) returns a 10x10 array of floats, with each element initialized to 1.0 . If x is an object reference, all elements will refer to the same object. fill(Foo(), dims) will return an array filled with the result of evaluating Foo() once.


2 Answers

The end keyword is only given this kind of special treatment inside of indexing expressions, where it evaluates to the last index of the dimension being indexed. You could put it inside with e.g.

for i in 0:n:nsamples-1
    window = a[i+1:min(i+n, end)]
end

Or you could just use length(a) (or nsamples, I guess they are the same?) instead of end to make it clear which end you are referring to.

like image 133
Toivo Henningsson Avatar answered Sep 30 '22 14:09

Toivo Henningsson


Ugly way:

a=rand(7);
nsamples=7;
n=3;
for i in 0:n:nsamples-1
    end_ = i+n < nsamples ? i+n : :end
    window = @eval a[$i+1:$end_]
    println(window)
end

Better solution:

for i in 0:n:nsamples-1
    window = i+n < nsamples ? a[i+1:i+n] : a[i+1:end]
    println(window)
end
like image 42
Reza Afzalan Avatar answered Sep 30 '22 15:09

Reza Afzalan