Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get the size of an array in Julia?

Tags:

arrays

julia

I'm trying to run the following piece of code:

function inversePoly(A::Array{Int64,1}, B::Array{Int64,1})
    n = size(A)
    retVal = A[end] / B[end]
    i = 1
    while i != n
        retVal = (retVal + 1 / B[n - i]) * A[n - i]
        i += 1
    end
    return retVal
end

inversePoly(Array(3:4), Array(4:5))

However, Julia gives me the following error:

LoadError: MethodError: no method matching -(::Tuple{Int64}, ::Int64)
Closest candidates are:
  -(!Matched::Complex{Bool}, ::Real) at complex.jl:298
  -(!Matched::Missing, ::Number) at missing.jl:97
  -(!Matched::Base.CoreLogging.LogLevel, ::Integer) at logging.jl:107
  ...
in expression starting at /home/francisco/Julia/abc.jl:12
inversePoly(::Array{Int64,1}, ::Array{Int64,1}) at abc.jl:6
top-level scope at none:0

The 6th line would be

retVal = (retVal + 1 / B[n - i]) * A[n - i]

This means that the statement

n = size(A)

Is saving a tuple in the variable n instead of an integer

How can I get an integer representing the number of elements in A?

Thanks in advance

like image 435
Francisco José Letterio Avatar asked Aug 15 '19 19:08

Francisco José Letterio


People also ask

How do I get the size of an array?

To determine the size of your array in bytes, you can use the sizeof operator: int a[17]; size_t n = sizeof(a); On my computer, ints are 4 bytes long, so n is 68. To determine the number of elements in the array, we can divide the total size of the array by the size of the array element.

What is size () in Julia?

size(A, [dim... ]) Returns a tuple containing the dimensions of A . Optionally you can specify the dimension(s) you want the length of, and get the length of that dimension, or a tuple of the lengths of dimensions you asked for.: julia> A = rand(2,3,4); julia> size(A, 2) 3 julia> size(A,3,2) (4,3)

How do you count in Julia?

The count() is an inbuilt function in julia which is used to count the number of elements in the specified array for which the given predicate p returns true and if p is omitted, counts the number of true elements in the given collection of boolean values.


1 Answers

Here is how you should use size:

julia> x = [1,2,3]
3-element Array{Int64,1}:
 1
 2
 3

julia> size(x)
(3,)

julia> size(x)[1]
3

julia> size(x, 1)
3

so either extract the first element from size(x) or directly specify which dimension you want to extract by passing 1 as a second argument.

In your case, as A is a Vector (it is single dimensional) you can also use length:

julia> length(x)
3

Which gives you an integer directly.

The difference between length and size is the following:

  • length is defined for collections (not only arrays) and returns an integer which gives you the number of elements in the collection
  • size returns a Tuple because in general it can be applied to multi-dimensional objects, in which case the tuple has as many elements as there are dimensions of the object (so in case of Vector, as in your question, it is 1-element tuple)
like image 99
Bogumił Kamiński Avatar answered Sep 28 '22 07:09

Bogumił Kamiński