Reputation: 58
I'm new to julia and have a problem. I have an array, y.
y = rand (5, 10)
y [1. 1] = 0
Running this gives me an error
for j=1:d
x_filt [j, 1] = y [j, findfirst (y [j, :])]
end
ERROR: syntax: missing separator in array expression
But this doesn't
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
Can someone explain how to make the first version work? Or at least explain why it doesn't?
Thanks!
Upvotes: 0
Views: 686
Reputation: 2699
First, I guess you meant y[1, 1] = 0
? I get an error if I use y [1. 1] = 0
.
Julia has space sensitive syntax in some contexts, notable inside brackets []
.
Some examples:
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
In your first example, the call to findfirst
in
x_filt [j, 1] = y [j, findfirst (y [j, :])]
is interpreted as two space-separated items, findfirst
and (y [j, :])]
. Julia then complains that they are separated by a space and not a comma.
In your second example, you were able to circumvent this since the call to findfirst
in
temp = findfirst (y [j, :])
is no longer in a space sensitive context.
I would recommend that when writing Julia code, you should never put a space between the function name and parenthesis (
in a function call or the variable and bracket [
in indexing, because the code will be treated differently in space sensitive contexts. E.g., your first example without the extra spaces
for j=1:d
x_filt[j, 1] = y[j, findfirst(y[j, :])]
end
works fine (provided that you define d
and x_filt
appropriately first).
Upvotes: 7