朱莉娅方法错误转换复杂{Float64} [英] Julia Method Error converting Complex{Float64}

查看:72
本文介绍了朱莉娅方法错误转换复杂{Float64}的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Julia的新手,并且我有以下代码出现此错误:

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.

这是我的代码:

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

由于这是Julia的第一天编程,所以我非常努力地揭示了错误的根源,但没有成功.我还尝试了尽可能地优化和初始化数组,但是当我对代码进行计时时,我意识到它远非最佳.感谢您的帮助.

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.

推荐答案

尝试这个简单得多的代码

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()

在您正确地进行工作之前,我不会太担心优化.现在设置的方式由于数组的所有切片而显得效率低下-最好尝试直接构建symbol.

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.

这篇关于朱莉娅方法错误转换复杂{Float64}的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆