liveBook · Manning

Vector

As explained in Julia as a Second Language

Vector is Julia's parametric one-dimensional array type for elements of a specified type.

A vector is Julia’s one-dimensional array: an ordered collection of elements accessed by index. It is useful for practical data such as pizza quantities or prices, and Julia starts indexing at 1. section 4.13 section 4.2

Keep these distinctions in mind when building array-based data:

  • Write elements separated by commas to create a column vector; separating them with spaces creates a one-row matrix instead. section 4.2
  • Use vectors for ordered one-dimensional data and matrices for tables with numbered rows and columns. section 4.2 section 14.1
  • Apply functions such as sum, length, sort, and sort! to summarize, measure, order, or modify vector contents. section 4.3
  • Use dotted operators such as .*, .+, .-, and ./ for elementwise arithmetic between vectors. section 4.13 section 4.3

Calculating pizza revenue

Store quantities and prices in separate vectors, then multiply matching elements and reduce the result: amounts = [4, 1, 5, 3, 2]; prices = [15.0, 11.5, 13.0, 12.75, 14.25]; revenue = sum(amounts .* prices), producing 203.25. A plain amounts * prices raises a method error because vector multiplication is not automatically defined here. section 4.3

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

Inspect a Julia vector directly in the REPL and see its length, element type, and vertically displayed values. section 4.2

How Julia as a Second Language covers it

How deep it goes
Explains construction, indexing, column-row distinctions, common functions, mutation conventions, and elementwise arithmetic, while deferring deeper multidimensional work.
Examples
Uses pizza quantities and prices to demonstrate construction, averaging, sorting, taxation, elementwise multiplication, and total revenue.
What it assumes
Assumes basic Julia array operations while teaching vector dimensions, indexing, and elementwise arithmetic through concrete examples.
What you can do after
Apply Julia vectors via indexing, sum, length, sort!, and dotted arithmetic to calculate and transform ordered data.
How it connects
Connects vectors with arrays, matrices, scalars, tuples, strings, linear algebra, mapping, filtering, and reduction.

Also explained in

Compare how 11 books explain vector →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Julia as a Second Language. The section links point to the parts of the book it draws on.