【发布时间】:2021-04-12 23:29:37
【问题描述】:
我对 Julia 有点陌生,我正在尝试使用 fill! method 来提高 Julia 的代码性能。目前,我从read_array 文件中读取了一个二维数组,并对其执行行操作以获得processed_array,如下所示:
function preprocess(matrix)
# Initialise
processed_array= Array{Float64,2}(undef, size(matrix));
#first row of processed_array is the difference of first two row of matrix
processed_array[1,:] = (matrix[2,:] .- matrix[1,:]) ;
#last row of processed_array is difference of last two rows of matrix
processed_array[end,:] = (matrix[end,:] .- matrix[end-1,:]);
#all other rows of processed_array is the mean-difference of other two rows
processed_array[2:end-1,:] = (matrix[3:end,:] .- matrix[1:end-2,:]) .*0.5 ;
return processed_array
end
但是,当我尝试使用 fill! 方法时,我得到了 MethodError。
processed_array = copy(matrix)
fill!(processed_array [1,:],d[2,:]-d[1,:])
MethodError: Cannot convert an object of type Matrix{Float64} to an object of type Float64
如果有人能告诉我我缺少什么并建议一种优化代码的方法,我会很高兴。提前致谢!
【问题讨论】:
标签: arrays optimization multidimensional-array julia