gpt4 book ai didi

Julia :与数组的 bool 比较

转载 作者:行者123 更新时间:2023-12-04 07:20:16 26 4
gpt4 key购买 nike

我有一个关于Julia中 bool 比较的简单问题。如何将以下Matlab代码转换为Julia?

Matlab:

% create parameters
o = -3;
mat = [65 -4; 65 -3; 65 -2]

% determine which rows of matrix have column 2 less than o AND column 1 equal to 65.
result = (o < mat(:,2) & mat(:,1) == 65)

我在Julia中尝试了以下方法:
# create parameters
o = -3
mat = zeros(3,2)
mat[:,1] = 65
mat[1,2] = -4
mat[2,2] = -3
mat[3,2] = -2
mat

# attempt to create desired result
o .< mat[:,2] # this part works
mat[:,1] .== 65 # this part works
test = (o .< mat[:,2] && mat[:,1] .== 65) # doesn't work
test = (o .< mat[:,2] .& mat[:,1] .== 65) # doesn't work
test = (o .< mat[:,2] & mat[:,1] .== 65) # doesn't work

最佳答案

这是运算符优先级的问题。在Julia中&的优先级高于在Matlab中。只需在括号内移动即可:

test = (o .< mat[:,2]) .& (mat[:,1] .== 65)

有关更多详细信息,请参见手册中的 Noteworthy differences from Matlab(也值得一读其他差异)。

关于 Julia :与数组的 bool 比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32639943/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com