Julia Method Error converting Complex{Float64}

霸气de小男生 提交于 2019-12-24 06:44:52

问题


I'm novice to Julia and I have the following code with this error:

MethodError(convert,(Complex{Float64},[-1.0 - 1.0im])).

I would like to know the source of the error and how to optimize this piece of code for speed.

This is my code:

function OfdmSym()
N = 64
n = 1000
symbol = convert(Array{Complex{Float64},2},ones(n,64))    # I need Array{Complex{Float64},2}
data   = convert(Array{Complex{Float64},2},ones(1,48))    # I need Array{Complex{Float64},2}

const unused = convert(Array{Complex{Float64},2},zeros(1,12))
const pilot  = convert(Array{Complex{Float64},2},ones(1,4))
const s      = convert(Array{Complex{Float64},2},[-1-im -1+im 1-im 1+im])# QPSK Complex Data

for i=1:n                  # generate 1000 symbols
    for j = 1:48           # generate 48 complex data symbols whose basis is s
          r = rand(1:4,1)  # 1, 2, 3, or 4
          data[j] = s[r]
    end
    symbol[i,:]=[data[1,1:10] pilot[1] data[1,11:20] pilot[2] data[1,21:30] pilot[3] data[1,31:40] pilot[4] data[1,41:48] unused]
end
end

As it's the first day programming in Julia, I tried very hard to reveal the source of the error without success. I also tried to optimize and initialize arrays as I could but when I time the code I realize that it is far from optimal. I appreciate your help.


回答1:


Try this much simpler code

function OfdmSym()
    N = 64
    n = 1000
    symbol = ones(Complex{Float64}, n, 64)
    data   = ones(Complex{Float64}, 1, 48)
    unused = zeros(Complex{Float64}, 1, 12)
    pilot  = ones(Complex{Float64}, 1, 4)
    s      = [-1-im  -1+im  1-im  1+im]

    for i=1:n                  # generate 1000 symbols
        for j = 1:48           # generate 48 complex data symbols whose basis is s
              r = rand(1:4)    # 1, 2, 3, or 4
              data[j] = s[r]
        end
        symbol[i,:]=[data[1,1:10] pilot[1] data[1,11:20] pilot[2] data[1,21:30] pilot[3] data[1,31:40] pilot[4] data[1,41:48] unused]
    end
end

OfdmSym()

I wouldn't worry too much about optimizing things until you have got it working correctly. The way you have it set up now seems like it'd be kinda inefficient due to all the slicing of arrays - it'd be better to try to build symbol directly.



来源:https://stackoverflow.com/questions/26086113/julia-method-error-converting-complexfloat64

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!