Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to index circular array in Julia?

I'd like to access elements in an array in a circular manner. Normally a modulo suffices but in Julia arrays start at 1. At the moment I'm basically converting the indices to a 0-based index and back. But this doesn't work for negative indices.

A = 1:5
for i in -6:6
    println(i, " -> ", ((i - 1) % length(A)) + 1)
end

Output

-6 -> -1 # wrong
-5 ->  0 # wrong
-4 ->  1 # wrong
-3 -> -3 # wrong
-2 -> -2 # wrong
-1 -> -1 # wrong
 0 ->  0 # wrong
 1 ->  1
 2 ->  2
 3 ->  3
 4 ->  4
 5 ->  5
 6 ->  1
like image 688
TomTom Avatar asked Dec 21 '19 11:12

TomTom


People also ask

Is Julia 1 or 0 indexed?

Conventionally, Julia's arrays are indexed starting at 1, whereas some other languages start numbering at 0, and yet others (e.g., Fortran) allow you to specify arbitrary starting indices.

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.

Are arrays mutable in Julia?

Arrays are mutable type collections in Julia, hence, their values can be modified with the use of certain pre-defined keywords. Julia allows adding new elements in an array with the use of push!

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 Index an array in Julia?

Indexing into arrays in Julia is similar to it’s counterparts. where each index_k may be a scalar integer, an array of integers, or an object that represents an array of scalar indices. Array Indexing in Julia is of two types: Cartesian Coordinates give the location of a point in 1D, 2D or 3D plane.

What types can be indexed in Julia?

Every type inside of Julia can be indexed, believe it or not. This is the case even for types that are not collections, and we will demonstrate this. However, let us first take a look at the simpler things, such as basic array indexing. We can index using an integer as a 1-dimensional reference point in the array.

What is the input value of array3 in Julia?

Input : array3 = [ 22, 7, 91, 69, 2, 74 ] sch = 6 Output : Element not found. [Note: Unlike other languages, In Julia indexing of an Array starts with 1.]

How to find all occurrences of an array in Julia?

[Note: Unlike other languages, In Julia indexing of an Array starts with 1.] The findall () method is used to find all the occurrences of the required element from an array, and return an Array (or CartesianArray, depending on the dimensions of input array) of the index, where the elements are present in the input array.


Video Answer


2 Answers

I usually use mod1 function for this. Here is an example:

julia> [-10:10 mod1.(-10:10, 5)]
21×2 Array{Int64,2}:
 -10  5
  -9  1
  -8  2
  -7  3
  -6  4
  -5  5
  -4  1
  -3  2
  -2  3
  -1  4
   0  5
   1  1
   2  2
   3  3
   4  4
   5  5
   6  1
   7  2
   8  3
   9  4
  10  5
like image 120
Bogumił Kamiński Avatar answered Oct 20 '22 17:10

Bogumił Kamiński


The % operator is not the mod operator as you suspect, but the rem operator.

Replace your modulus operation with mod( i-1, length(A) ) and you will get your intended result.


PS. I would have to add, the use-case for such circularity also matters. If you're trying to replicate python-like negative indices, where a negative index of -1 indexes the last element of the array, and continuing circularly in that fashion from that point on for negative numbers, would necessarily require a different treatment for 0 and two different branches, one for positive and one for negative numbers.

like image 31
Tasos Papastylianou Avatar answered Oct 20 '22 17:10

Tasos Papastylianou