Skip to content

Commit 38b9682

Browse files
committed
Some small changes
1 parent 2b389e5 commit 38b9682

File tree

1 file changed

+61
-30
lines changed

1 file changed

+61
-30
lines changed

docs/src/lecture_02/arrays.md

Lines changed: 61 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ Create a vector of positive integers that contains all odd numbers smaller than
245245

246246
Such a vector can be either created manually by
247247

248-
```jldoctest matrices
248+
```jldoctest vectors_ex
249249
julia> v = [1,3,5,7,9]
250250
5-element Vector{Int64}:
251251
1
@@ -257,7 +257,7 @@ julia> v = [1,3,5,7,9]
257257

258258
or we can use the `range` function to create a range with given properties and then use the `collect` function to create a vector. Another possibility is to use the `Vector` type to convert the range into a vector.
259259

260-
```jldoctest matrices
260+
```jldoctest vectors_ex
261261
julia> collect(1:2:9)
262262
5-element Vector{Int64}:
263263
1
@@ -277,7 +277,7 @@ julia> Vector(1:2:9)
277277

278278
The values stored in the vector can be changed using the `.=` sign and proper indices. Do not forget to add the dot before the `=` sign to perform the element-wise operation.
279279

280-
```jldoctest matrices
280+
```jldoctest vectors_ex
281281
julia> v[1] = 4
282282
4
283283
@@ -304,7 +304,7 @@ julia> v
304304
A matrix is a special case of an array with precisely two dimensions. In Julia, we can construct a matrix by the square brackets similarly to vectors. Matrices are built row by row. Elements in rows are separated by spaces, and rows are separated by semicolons.
305305

306306
```jldoctest matrices
307-
julia> m = [1 2 3 4; 5 6 7 8]
307+
julia> M = [1 2 3 4; 5 6 7 8]
308308
2×4 Matrix{Int64}:
309309
1 2 3 4
310310
5 6 7 8
@@ -313,69 +313,69 @@ julia> m = [1 2 3 4; 5 6 7 8]
313313
The same functions can obtain the basic information about matrices as for vectors.
314314

315315
```jldoctest matrices
316-
julia> typeof(m)
316+
julia> typeof(M)
317317
Matrix{Int64} (alias for Array{Int64, 2})
318318
319-
julia> eltype(m)
319+
julia> eltype(M)
320320
Int64
321321
322-
julia> ndims(m)
322+
julia> ndims(M)
323323
2
324324
325-
julia> size(m)
325+
julia> size(M)
326326
(2, 4)
327327
328-
julia> length(m)
328+
julia> length(M)
329329
8
330330
```
331331

332332
Accessing matrix elements can be also done in the same way as for vectors.
333333

334334
```jldoctest matrices
335-
julia> m[1] # the first element, equivalent to m[begin]
335+
julia> M[1] # the first element, equivalent to m[begin]
336336
1
337337
338-
julia> m[2] # the second element
338+
julia> M[2] # the the second element element
339339
5
340340
341-
julia> m[end-1] # the last element
341+
julia> M[end-1] # the second to last element
342342
4
343343
```
344344

345345
Note that the second element is `5`. The reason is that Julia is column-oriented. Element at a specific position in a matrix can be accessed by the following syntax `matrix[row_index, column_index]`. The following code returns the second element in the first row.
346346

347347
```jldoctest matrices
348-
julia> m[1, 2]
348+
julia> M[1, 2]
349349
2
350350
```
351351

352352
It is also possible to access multiple elements at once
353353

354354
```jldoctest matrices
355-
julia> m[1, [2, 3]] # the second and third element in the first row
355+
julia> M[1, [2, 3]] # the second and third element in the first row
356356
2-element Vector{Int64}:
357357
2
358358
3
359359
360-
julia> m[1:3] # the first three elements according to linear indexing
360+
julia> M[1:3] # the first three elements according to linear indexing
361361
3-element Vector{Int64}:
362362
1
363363
5
364364
2
365365
366-
julia> m[:, 1:3] # the first three columns
366+
julia> M[:, 1:3] # the first three columns
367367
2×3 Matrix{Int64}:
368368
1 2 3
369369
5 6 7
370370
371-
julia> m[1, :] # the first row
371+
julia> M[1, :] # the first row
372372
4-element Vector{Int64}:
373373
1
374374
2
375375
3
376376
4
377377
378-
julia> m[:] # all elements
378+
julia> M[:] # all elements
379379
8-element Vector{Int64}:
380380
1
381381
5
@@ -390,7 +390,7 @@ julia> m[:] # all elements
390390
It is impossible to append new elements into arrays directly, except for vectors. However, arrays with matching sizes along a dimension can be concatenated in this dimension. For example, we can horizontally concatenate the matrix `m` using the `hcat` function.
391391

392392
```jldoctest matrices
393-
julia> hcat(m, m)
393+
julia> hcat(M, M)
394394
2×8 Matrix{Int64}:
395395
1 2 3 4 1 2 3 4
396396
5 6 7 8 5 6 7 8
@@ -399,7 +399,7 @@ julia> hcat(m, m)
399399
For concatenating vertically, we use the `vcat` function.
400400

401401
```jldoctest matrices
402-
julia> vcat(m, m)
402+
julia> vcat(M, M)
403403
4×4 Matrix{Int64}:
404404
1 2 3 4
405405
5 6 7 8
@@ -410,12 +410,12 @@ julia> vcat(m, m)
410410
The general function `cat` concatenates arrays along the dimension specified by the `dims` keyword argument.
411411

412412
```jldoctest matrices
413-
julia> cat(m, m; dims = 2) # equivalent to hcat(m, m)
413+
julia> cat(M, M; dims = 2) # equivalent to hcat(m, m)
414414
2×8 Matrix{Int64}:
415415
1 2 3 4 1 2 3 4
416416
5 6 7 8 5 6 7 8
417417
418-
julia> cat(m, m; dims = 1) # equivalent to vcat(m, m)
418+
julia> cat(M, M; dims = 1) # equivalent to vcat(m, m)
419419
4×4 Matrix{Int64}:
420420
1 2 3 4
421421
5 6 7 8
@@ -431,7 +431,12 @@ julia> v = [11, 12]
431431
11
432432
12
433433
434-
julia> vcat(m, v)
434+
julia> hcat(M, v)
435+
2×5 Matrix{Int64}:
436+
1 2 3 4 11
437+
5 6 7 8 12
438+
439+
julia> vcat(M, v)
435440
ERROR: ArgumentError: number of columns of each array must match (got (4, 1))
436441
[...]
437442
```
@@ -473,7 +478,7 @@ julia> v2 = collect(2:2:10)
473478
Then we use the `hcat` function to concatenate these two vectors horizontally.
474479

475480
```jldoctest matrices_ex
476-
julia> m = hcat(v1, v2)
481+
julia> M = hcat(v1, v2)
477482
5×2 Matrix{Int64}:
478483
1 2
479484
3 4
@@ -485,12 +490,12 @@ julia> m = hcat(v1, v2)
485490
Finally, we select all elements in the third row and assign the new value to them.
486491

487492
```jldoctest matrices_ex
488-
julia> m[3,:] .= 4
493+
julia> M[3,:] .= 4
489494
2-element view(::Matrix{Int64}, 3, :) with eltype Int64:
490495
4
491496
4
492497
493-
julia> m
498+
julia> M
494499
5×2 Matrix{Int64}:
495500
1 2
496501
3 4
@@ -507,6 +512,31 @@ julia> m
507512

508513
In many cases, it is useful to use arrays with more dimensions to store data. As an example, we can mention RGB images, which are typically stored in `3`-dimensional arrays. In Julia, there is no straightforward way to create `N`-dimensional arrays. The typical way to make such an array is to create an empty array of appropriate size and then fill it manually or using a loop. In this lecture, we will focus only on the basics of creating arrays. The lecture focused on [loops](@ref for-and-while-loops) will explain this topic in more details.
509514

515+
!!! compat "New features in Julia 1.7"
516+
Starting with Julia 1.7, it is possible to create multidimensional arrays in a similar way to matrices and vectors. Repeated semicolons can be used inside array concatenation expressions to separate dimensions of an array, with the number of semicolons specifying the dimension.
517+
```julia
518+
julia> [1; 2; 3]
519+
3-element Vector{Int64}:
520+
1
521+
2
522+
3
523+
524+
julia> [1;; 2;; 3]
525+
1×3 Matrix{Int64}:
526+
1 2 3
527+
528+
julia> [1;;; 2;;; 3]
529+
1×1×3 Array{Int64, 3}:
530+
[:, :, 1] =
531+
1
532+
533+
[:, :, 2] =
534+
2
535+
536+
[:, :, 3] =
537+
3
538+
```
539+
510540
There are several ways to initialize an array. The simplest and most common is using the `zeros` function. By default, this function creates an array of given size filled with zeros of type `Float64`.
511541

512542
```jldoctest arrays
@@ -601,10 +631,11 @@ julia> ones(Float32, 2, 3, 1)
601631
Function `fill` creates an array of given size filled with the given value.
602632

603633
```jldoctest
604-
julia> fill(1.234, 2, 2)
605-
2×2 Matrix{Float64}:
606-
1.234 1.234
607-
1.234 1.234
634+
julia> fill(1.234, 2, 3, 1)
635+
2×3×1 Array{Float64, 3}:
636+
[:, :, 1] =
637+
1.234 1.234 1.234
638+
1.234 1.234 1.234
608639
```
609640

610641
```@raw html

0 commit comments

Comments
 (0)