我是朱莉娅的新手并且有问题.我有一个数组,y.
y = rand (5, 10)
y [1. 1] = 0
Run Code Online (Sandbox Code Playgroud)
运行这个给我一个错误
for j=1:d
x_filt [j, 1] = y [j, findfirst (y [j, :])]
end
ERROR: syntax: missing separator in array expression
Run Code Online (Sandbox Code Playgroud)
但事实并非如此
for j=1:d # fix to 1st obs if 1st tick is missing
temp = findfirst (y [j, :])
x_filt [j, 1] = y [j, temp];
end
Run Code Online (Sandbox Code Playgroud)
有人可以解释如何使第一个版本工作?或者至少解释为什么不呢?
谢谢!
首先,我想你的意思是y[1, 1] = 0
?如果我使用,我会收到错误y [1. 1] = 0
.
Julia 在某些情况下具有空间敏感语法,在括号内显着[]
.一些例子:
julia> max(1, 2)
2
julia> max (1, 2)
2
julia> [max(1, 2)]
1-element Array{Int64,1}:
2
julia> [max (1, 2)]
1x2 Array{Any,2}:
max (1,2)
julia> [1 + 2]
1-element Array{Int64,1}:
3
julia> [1 +2]
1x2 Array{Int64,2}:
1 2
Run Code Online (Sandbox Code Playgroud)
在您的第一个示例中,调用findfirst
in
x_filt [j, 1] = y [j, findfirst (y [j, :])]
Run Code Online (Sandbox Code Playgroud)
被解释为两个空格分隔的项目,findfirst
和(y [j, :])]
.朱莉娅然后抱怨他们被一个空格而不是一个逗号分开.
在第二个例子中,你能够规避这一点,因为在调用findfirst
中
temp = findfirst (y [j, :])
Run Code Online (Sandbox Code Playgroud)
不再处于空间敏感的环境中.
我建议在编写Julia代码时,不要(
在函数调用中的函数名和括号之间或[
索引中的变量和括号之间放置空格,因为在空间敏感的上下文中代码将被区别对待.例如,你的第一个例子没有额外的空格
for j=1:d
x_filt[j, 1] = y[j, findfirst(y[j, :])]
end
Run Code Online (Sandbox Code Playgroud)
工作正常(前提是你定义d
并x_filt
适当地首先).