ERROR: LoadError: MethodError: no method matching ./(::#sigma2#1, ::Array{Float64,2})

What is wrong with the following code?

for i=1:length(m)	# loop over the various cluster sizes
  for k=1:N-2*m[i]	# implements the summation in the Allan Variance  equation (eqn. 8 in ref 1.
    sigma2(i,:) = sigma2(i,:) + (theta(k+2*m[i],:) - 2*theta(k+m[i],:) + theta(k,:)).^2
    end
end

sigma2 = sigma2./repmat((2*T.^2.*(N-2*m)),1,M)

I get the error message

ERROR: LoadError: MethodError: no method matching ./(::#sigma2#1, ::Array{Float64,2})
Closest candidates are:
./{T}(!Matched::Number, ::AbstractArray{T,N}) at arraymath.jl:67
./(!Matched::SparseMatrixCSC{Tv,Ti<:Integer}, ::Array{T,N}) at sparse/sparsematrix.jl:1724
./(!Matched::AbstractArray{T,N}, ::AbstractArray{T,N}) at broadcast.jl:302

This is julia and not MATLAB so you should use [] for indexing.

1 Like

Right, thanks

However,

for i=1:length(m)	# loop over the various cluster sizes
  for k=1:N-2*m[i]	# implements the summation in the Allan Variance  equation (eqn. 8 in ref 1.
    sigma2[i,:] = sigma2[i,:] + (theta[k+2*m[i],:] - 2*theta[k+m[i],:] + theta[k,:]).^2
    end
end

throws

ERROR: LoadError: ArgumentError: invalid index: 3.0
in macro expansion at ./multidimensional.jl:294 [inlined]
in _unsafe_getindex at ./multidimensional.jl:291 [inlined]
in _getindex at ./multidimensional.jl:271 [inlined]
in getindex at ./abstractarray.jl:760 [inlined]

http://docs.julialang.org/en/latest/manual/noteworthy-differences.html#Noteworthy-differences-from-MATLAB-1

Looks like you you’re indexing an Array with an Float (i.e: 1.0, 2.1) when what you want is an Integer (i.e: 1, 2, etc)

Some documentation on that: http://docs.julialang.org/en/stable/manual/integers-and-floating-point-numbers.

what is the output of:

typeof(m)

1 Like

Hmmm, somehow it’s not mentioned there. You cannot index an array with floating point numbers. Without the full code it’s impossible to tell what you are doing but it’s likely that m is not an integer array.

typeof(m) Array{Float64,2}

m= [1.0 4.0 16.0 64.0 256.0]

I think m is meant to be intergers

(and yes, the code is an attempt to port from MATLAB)

Here is the whole code:

# ********* FUNCTION DEFINITIONS ********

function allan(omega,fs,pts)
# omega = sample data. Unit should be in radians/sec
# fs 	= sample frequency
# pts 	= number of logarithmically spaced points 

# Determine the size of the output data set 
(N,M) = size(omega)

# determine largest bin size
n = 2.^(0:floor(log2(N/2)))'


# Select the last element of the vector (the largest bin size)
maxN = n[end]
endLogInc = log10(maxN)

# m is chosen arbitrarily
m = unique(ceil(logspace(0,endLogInc,pts)))'   # create log spaced vector average factor 

# t0 = sample interval
t0 = 1/fs	

# T = length of time of each cluster (tau)
T = m*t0	

# Integrate samples over time to obtain output angle θ
theta = cumsum(omega)/fs

# Create an array of dimensions (cluster periods) X ( No. of variables)
sigma2 = zeros(length(T),M)

for i=1:length(m)	# loop over the various cluster sizes
  for k=1:N-2*m[i]	# implements the summation in the Allan Variance  equation (eqn. 8 in ref 1.
    sigma2[i,:] = sigma2[i,:] + (theta[k+2*m[i],:] - 2*theta[k+m[i],:] + theta[k,:]).^2
    end
end

sigma2 = sigma2./repmat((2*T.^2.*(N-2*m)),1,M)
sigma = sqrt(sigma2) # The Allan Deviation 

return sigma
end

# ********* END OF FUNCTION DEFINITIONS *****

# ********* MAIN PROGRAM ********

file=open("sensor_data.dat", "r")
data = readdlm(file,' ')
close(file)

fs = 50 # 50 Hz Sample rate
pts = 5 # No. of logarithmically spaced points

Sigma = allan(data, fs, pts)

println("Sigma= ", Sigma)


# ********* END OF MAIN PROGRAM ********

In Julia, the ceil() function rounds upward but does not convert the result to an integer:

julia> ceil([1.5, 1.6])
2-element Array{Float64,1}:
 2.0
 2.0

to get an integer, you can instead do:

julia> ceil(Int, [1.5, 1.6])
2-element Array{Int64,1}:
 2
 2

Many Julia functions take a type as their first parameter like this:

julia> zeros(Int, 2, 2)
2Ă—2 Array{Int64,2}:
 0  0
 0  0

julia> rand(Int, 2, 2)
2Ă—2 Array{Int64,2}:
 4442662091972791053  1315597098779686556
  -67815619393417912  8004661696684852519
3 Likes

Thank you! That did the trick

I have one last problem,

What is the essence of this expression in MATLAB ?

sigma2 = sigma2./repmat((2*T.^2.*(N-2*m)),1,M)

It seems like the syntax would work in Julia as well. But when I execute this line I get the error:

ERROR: LoadError: DimensionMismatch(“arrays could not be broadcast to a common size”)

The nominator and denominator have the following properties

typeof(sigma2)= Array{Float64,2}

typeof(repmat((2T.^2.(N-2*m)),1,M))= Array{Float64,2}

sizeof(sigma2)= 440

sizeof(repmat((2T.^2.(N-2*m)),1,M))= 440

sigma2= [13.2508 11.556 8.442 46.3152 44.0672 28.2076 94.5208 125.92 924.021 5.8616 0.2104; 45.6592 34.5664 30.036 253.931 225.472 107.641 438.842 586.606 4502.78 50.814 1.6468; 184.576 110.935 166.279 1053.09 1225.07 413.82 2375.03 2487.8 32825.9 706.581 12.8648; 2082.21 1868.43 10988.8 4948.69 4457.05 1274.72 10652.1 6671.7 63628.1 7758.94 199.189; 1.05607e5 33235.0 37830.9 814.318 1968.7 7961.28 1.00828e5 39766.8 59889.7 7902.17 963.78]

repmat((2T.^2.(N-2*m)),1,M)= [0.5888 9.344 144.589 1998.85 11848.9 0.5888 9.344 144.589 1998.85 11848.9 0.5888 9.344 144.589 1998.85 11848.9 0.5888 9.344 144.589 1998.85 11848.9 0.5888 9.344 144.589 1998.85 11848.9 0.5888 9.344 144.589 1998.85 11848.9 0.5888 9.344 144.589 1998.85 11848.9 0.5888 9.344 144.589 1998.85 11848.9 0.5888 9.344 144.589 1998.85 11848.9 0.5888 9.344 144.589 1998.85 11848.9 0.5888 9.344 144.589 1998.85 11848.9]

Hm, looks like those arrays are not actually the same size.

sizeof and size are not the same commands, see below documentation for both:

help?> sizeof
search: sizeof

sizeof(T)

Size, in bytes, of the canonical binary representation of the given DataType T, if any.

sizeof(s::AbstractString)

The number of bytes in string s.

and:

help?> size
search: size sizeof sizehint! Csize_t resize! filesize Cssize_t trailingsize serialize Serializer deserialize displaysize

size(A::AbstractArray, [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 = ones(2,3,4);

julia> size(A, 2)
3

julia> size(A,3,2)
(4,3)

Showing the output of size should help you debug this problem.