I have the following code, where given i I want to find the ith row of a matrix. My code is the following:
function f(mat,i)
println(mat[:i,:])
end
However, I get the following error:
ArgumentError: invalid index: :i of type Symbol
I have tried printing the type of i using typeof and it says it is Int64. Further, if I attempt to just find the first row then mat[:1,:] does the job so I don't think the problem is with the slicing syntax.
You can get e.g. the first row of a matrix like this:
Note that normally you just pass a row number (in my case
1) to signal the row you want to fetch. In this case as a result you get aVector.However, you can use slicing
1:1which gets a 1-element range of rows. In this case the result is aMatrixhaving one row.Now the issue of
:1. See below:As you can see
:1is just the same as1. However, e.g.:xis a special type calledSymbol. Its most common use is to represent field names in structs. Since field names cannot start with a number (variable names in Julia, as also in other programming languages) have to start with something else, e.g. a letterxas in my example, there is no ambiguity here. Putting:in front of a number is a no-op, while putting it in front of a valid variable identifier creates aSymbol. See the help forSymbolin the Julia REPL for more examples.In Julia ranges always require passing start and end i.e.
a:bis a range starting fromaand ending withbinclusive, examples: