0

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!

1 Answer 1

7

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).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.