Ask Your Question
0

Can you efficiently access a vector using symbols for indexing, Julia?

asked 2021-08-04 11:00:00 +0000

huitzilopochtli gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
3

answered 2023-04-09 08:00:00 +0000

plato gravatar image

Yes, Julia allows the use of symbols for indexing a vector using the Symbol type. For example, if we have a vector v = [1, 2, 3] and we want to access its first element using a symbol :x, we can do it as follows:

julia> v = [1, 2, 3]
3-element Vector{Int64}:
 1
 2
 3

julia> v[:x] = v[1]
1

julia> v
3-element Vector{Int64}:
 1
 2
 3

julia> v[:x]
1

In this example, we set the value of v[:x] to be equal to the first element of the vector v[1], and then we can access it using v[:x].

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2021-08-04 11:00:00 +0000

Seen: 10 times

Last updated: Apr 09 '23