Julia一维数组使用报错:no method matching setindex!(::typeof(split), ::Float64, ::Int64)

Julia报错一维数组使用:

报错信息:no method matching setindex!(::typeof(split), ::Float64, ::Int64)

代码功能描述:根据给定任务总数和设置的cpu核数 进行任务分配,返回任务分配int[]数组

代码示例:

# @title splitInt
# @descroption 根据给定任务总数和设置的cpu核数 进行任务分配,返回任务分配int[]数组
# @auth	张奥男	(2022/09/27)
# @param	numPoints	int		"任务总数"
# @param	numChunks	int		"设置的cpu核数"
# @return 	split	[]int		"任务分配表"
#	error     			"函数调用信息"
function splitInt(numPoints::Int, numChunks::Int)
	if numPoints%numChunks == 0 
		#for i := 0; i < numChunks; i++ {
        for i in 1: numChunks
			split[i] = numPoints ÷ numChunks
        end
	else
		limit = numPoints % numChunks
		for j in 1: limit
			split[j] =numPoints÷numChunks + 1
        end
		for k in limit+1: numChunks 
			split[k] = numPoints ÷ numChunks
        end
    end
    print(split[2])
    #return split
end
splitInt(101,4)

报错信息:no method matching setindex!(::typeof(split), ::Float64, ::Int64)

错误原因:是因为我没有指定数组的类型

修改后的代码:

# @title splitInt
# @descroption 根据给定任务总数和设置的cpu核数 进行任务分配,返回任务分配int[]数组
# @auth	张奥男	(2022/09/27)
# @param	numPoints	int		"任务总数"
# @param	numChunks	int		"设置的cpu核数"
# @return 	split	[]int		"任务分配表"
#	error     			"函数调用信息"
function splitInt(numPoints::Int, numChunks::Int)
	#split := make([]int, numChunks)
    #split[] = {0,0,0,0}array = Array{Int64}(undef, 3)
    split = Array{Int64}(undef, numChunks)
	if numPoints%numChunks == 0 
		#for i := 0; i < numChunks; i++ {
        for i in 1: numChunks
			split[i] = numPoints ÷ numChunks
        end
	else
		limit = numPoints % numChunks
		for j in 1: limit
			split[j] =numPoints÷numChunks + 1
        end
		for k in limit+1: numChunks 
			split[k] = numPoints ÷ numChunks
        end
    end
    print(split[2])
    #return split
end
splitInt(101,4)

总结:上面错误的原因是,我没有声明 一维数组的类型,而Julia是支持一个数组存不同数据类型的,而在运算过程中有浮点数的出现,而我要求返回的是整型,故,系统报错。

Julia一维数组的声明:

split = Array{Int64}(undef, numChunks)  // 含义:Int64类型 一维 numChunks个大小
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

奥男

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值