You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/src/lecture_02/arrays.md
+61-30Lines changed: 61 additions & 30 deletions
Original file line number
Diff line number
Diff line change
@@ -245,7 +245,7 @@ Create a vector of positive integers that contains all odd numbers smaller than
245
245
246
246
Such a vector can be either created manually by
247
247
248
-
```jldoctestmatrices
248
+
```jldoctestvectors_ex
249
249
julia> v = [1,3,5,7,9]
250
250
5-element Vector{Int64}:
251
251
1
@@ -257,7 +257,7 @@ julia> v = [1,3,5,7,9]
257
257
258
258
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.
259
259
260
-
```jldoctestmatrices
260
+
```jldoctestvectors_ex
261
261
julia> collect(1:2:9)
262
262
5-element Vector{Int64}:
263
263
1
@@ -277,7 +277,7 @@ julia> Vector(1:2:9)
277
277
278
278
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.
279
279
280
-
```jldoctestmatrices
280
+
```jldoctestvectors_ex
281
281
julia> v[1] = 4
282
282
4
283
283
@@ -304,7 +304,7 @@ julia> v
304
304
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.
The same functions can obtain the basic information about matrices as for vectors.
314
314
315
315
```jldoctest matrices
316
-
julia> typeof(m)
316
+
julia> typeof(M)
317
317
Matrix{Int64} (alias for Array{Int64, 2})
318
318
319
-
julia> eltype(m)
319
+
julia> eltype(M)
320
320
Int64
321
321
322
-
julia> ndims(m)
322
+
julia> ndims(M)
323
323
2
324
324
325
-
julia> size(m)
325
+
julia> size(M)
326
326
(2, 4)
327
327
328
-
julia> length(m)
328
+
julia> length(M)
329
329
8
330
330
```
331
331
332
332
Accessing matrix elements can be also done in the same way as for vectors.
333
333
334
334
```jldoctest matrices
335
-
julia> m[1] # the first element, equivalent to m[begin]
335
+
julia> M[1] # the first element, equivalent to m[begin]
336
336
1
337
337
338
-
julia> m[2] # the second element
338
+
julia> M[2] # the the second element element
339
339
5
340
340
341
-
julia> m[end-1] # the last element
341
+
julia> M[end-1] # the second to last element
342
342
4
343
343
```
344
344
345
345
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.
346
346
347
347
```jldoctest matrices
348
-
julia> m[1, 2]
348
+
julia> M[1, 2]
349
349
2
350
350
```
351
351
352
352
It is also possible to access multiple elements at once
353
353
354
354
```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
356
356
2-element Vector{Int64}:
357
357
2
358
358
3
359
359
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
361
361
3-element Vector{Int64}:
362
362
1
363
363
5
364
364
2
365
365
366
-
julia> m[:, 1:3] # the first three columns
366
+
julia> M[:, 1:3] # the first three columns
367
367
2×3 Matrix{Int64}:
368
368
1 2 3
369
369
5 6 7
370
370
371
-
julia> m[1, :] # the first row
371
+
julia> M[1, :] # the first row
372
372
4-element Vector{Int64}:
373
373
1
374
374
2
375
375
3
376
376
4
377
377
378
-
julia> m[:] # all elements
378
+
julia> M[:] # all elements
379
379
8-element Vector{Int64}:
380
380
1
381
381
5
@@ -390,7 +390,7 @@ julia> m[:] # all elements
390
390
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.
391
391
392
392
```jldoctest matrices
393
-
julia> hcat(m, m)
393
+
julia> hcat(M, M)
394
394
2×8 Matrix{Int64}:
395
395
1 2 3 4 1 2 3 4
396
396
5 6 7 8 5 6 7 8
@@ -399,7 +399,7 @@ julia> hcat(m, m)
399
399
For concatenating vertically, we use the `vcat` function.
400
400
401
401
```jldoctest matrices
402
-
julia> vcat(m, m)
402
+
julia> vcat(M, M)
403
403
4×4 Matrix{Int64}:
404
404
1 2 3 4
405
405
5 6 7 8
@@ -410,12 +410,12 @@ julia> vcat(m, m)
410
410
The general function `cat` concatenates arrays along the dimension specified by the `dims` keyword argument.
411
411
412
412
```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)
414
414
2×8 Matrix{Int64}:
415
415
1 2 3 4 1 2 3 4
416
416
5 6 7 8 5 6 7 8
417
417
418
-
julia> cat(m, m; dims = 1) # equivalent to vcat(m, m)
418
+
julia> cat(M, M; dims = 1) # equivalent to vcat(m, m)
419
419
4×4 Matrix{Int64}:
420
420
1 2 3 4
421
421
5 6 7 8
@@ -431,7 +431,12 @@ julia> v = [11, 12]
431
431
11
432
432
12
433
433
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)
435
440
ERROR: ArgumentError: number of columns of each array must match (got (4, 1))
436
441
[...]
437
442
```
@@ -473,7 +478,7 @@ julia> v2 = collect(2:2:10)
473
478
Then we use the `hcat` function to concatenate these two vectors horizontally.
474
479
475
480
```jldoctest matrices_ex
476
-
julia> m = hcat(v1, v2)
481
+
julia> M = hcat(v1, v2)
477
482
5×2 Matrix{Int64}:
478
483
1 2
479
484
3 4
@@ -485,12 +490,12 @@ julia> m = hcat(v1, v2)
485
490
Finally, we select all elements in the third row and assign the new value to them.
486
491
487
492
```jldoctest matrices_ex
488
-
julia> m[3,:] .= 4
493
+
julia> M[3,:] .= 4
489
494
2-element view(::Matrix{Int64}, 3, :) with eltype Int64:
490
495
4
491
496
4
492
497
493
-
julia> m
498
+
julia> M
494
499
5×2 Matrix{Int64}:
495
500
1 2
496
501
3 4
@@ -507,6 +512,31 @@ julia> m
507
512
508
513
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.
509
514
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
+
510
540
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`.
0 commit comments