Sözlük

Soldaki anahtar kelimelerden birini seçin…

Programming in JuliaMultidimensional Arrays

Okuma zamanı: ~15 min

We've seen a couple exercises that involve dealing with matrices as an array of arrays. This gets quite tedious if you have to deal with matrices often, because many common tasks require custom methods with this approach (for example, simply selecting a column).

Since multidimensional arrays are very common in scientific computing, Julia has a built-in multidimensional array type. In other words, Julia arrays can be arranged in a rectangle or a cube, etc. The syntax for inputting a rectangular array involves separating rows with semicolons and row elements with spaces: A = [1 2 3; 4 5 6; 7 8 9]. Alternatively, you can use the newline character to separate rows:

A = [
1 2 3
4 5 6
7 8 9
]

We can find the dimensions of A using size(A). For example, the size of the matrix A defined above is . You can access particular dimensions with a second argument, like size(A,1) or size(A,2).

To index a multidimensional array, we use commas to separate selectors for each dimension. For example, A[2:3,:] selects the second row through the third row and all of the columns (the lone colon is short for 1:end).

Array comprehension syntax works with multidimensional arrays as well. Just separate the index iterators with a comma:

julia> [i^2 + j^2 for i in 1:3, j in 1:5]

3×5 Array{Int64,2}:
2   5  10  17  26
5   8  13  20  29
10  13  18  25  34

As you can see in the first line of the above output, the type of an array prints as Array{T,d} where T is the type of the array's entries and d is the number of dimensions.

Random arrays can be generated in Julia using rand (uniform in the interval [0,1]) or randn (standard normal distribution). These functions take an integer argument to specify the length of the output array.

rand(10) # a vector of ten Unif([0,1])'s
randn(10) # a vector of ten standard normals
rand([3,5,11],100) # a vector of 100 samples from the array [3,5,11]

The random number generator can be seeded to ensure it produces the same results when run repeatedly:

using Random
Random.seed!(123)
rand(), rand()

The two calls to rand yield , but if we run the whole block again, we will get .

Exercise
Succinctly generate the following two-dimensional array

\begin{align*}\begin{bmatrix} 0 & 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 & 0 \\ 2 & 3 & 4 & 0 & 1 \\ 3 & 4 & 0 & 1 & 2 \\ 4 & 0 & 1 & 2 & 3 \end{bmatrix}\end{align*}

store it to a variable, and write a line of code to select the submatrix

\begin{align*}\begin{bmatrix} 3 & 4 & 0 & 1 & 2 \\ 4 & 0 & 1 & 2 & 3 \end{bmatrix}\end{align*}

Hint: you might want to use the function rem—look it up from a Julia session to check how it works.

Solution. A = [rem(i+j,5) for i=0:4,j=0:4] generates the first matrix and stores it to the variable A. Then A[end-1:end,:] takes the last two rows of A.

Daha fazla içeriğe ulaşmak için yukarıdaki tüm aktivite ve egzersizleri tamamlamalısın. 
Sorun mu yaşıyorsun? ya da tüm adımları göster

Sıradaki:
Plotting
Bruno
Bruno Bruno