This example shows the basic methods and functions for working with matrices in Engee.
Firstly, let's define a vector of 9 elements.
Out[0]:
1×9 Matrix{Int64}:
1 2 3 4 6 4 3 9 10
Increase the value of each element by 2 and store the result in a new vector. In Julia, this is done with the operator .+
.
Out[0]:
1×9 Matrix{Int64}:
3 4 5 6 8 6 5 11 12
Let's plot the resulting vector-string b. For visualisation it is necessary to connect the library Plots.jl. And feed the column vector to the input of the function plot()
. To do this, transpose b using the operator '
.
Out[0]:
9×1 adjoint(::Matrix{Int64}) with eltype Int64:
3
4
5
6
8
6
5
11
12
We can also make another type of graph. For example, let's display the values of vector c in the form of a diagram. To do this, first call the function plotlyjs()
.
To create a rectangular matrix, you must specify the rows via ;
.
Out[0]:
3×3 Matrix{Int64}:
1 2 0
2 5 -1
4 10 -1
Transpose the matrix and write the result into another variable, for example B. Multiply matrix A with the resulting matrix B.
Out[0]:
3×3 adjoint(::Matrix{Int64}) with eltype Int64:
1 2 4
2 5 10
0 -1 -1
Out[0]:
3×3 Matrix{Int64}:
5 12 24
12 30 59
24 59 117
But we can also multiply each element of matrix A by each element of matrix B, as we did with vectors earlier. That is, the matrices will be multiplied not by the rule "row to column", but element by element.
Out[0]:
3×3 Matrix{Int64}:
1 4 0
4 25 -10
0 -10 1
Let us solve a simple equation of the following form
$$
A*x=b
$$
set $b$ as a column vector.
Out[0]:
3-element Vector{Int64}:
1
3
5
Out[0]:
3-element Vector{Float64}:
1.0
-0.0
-1.0
Out[0]:
3-element Vector{Float64}:
0.0
0.0
0.0
Matrix operations from the linear algebra section¶
The library LinearAlgebra
allows you to work with various matrix characterisations and matrix calculations. For example, let's find the eigenvalues of matrix A. To do this, we apply the function eigvals()
.
Out[0]:
3-element Vector{Float64}:
0.267949192431123
0.9999999999999999
3.732050807568875
Let us find the determinant of the given matrix by passing it as an input value to the function det()
.
It is also possible to perform singular decomposition of the matrix A, using the function svd()
.
Out[0]:
SVD{Float64, Float64, Matrix{Float64}, Vector{Float64}}
U factor:
3×3 Matrix{Float64}:
-0.179911 -0.521682 0.833954
-0.443397 0.799785 0.404652
-0.878084 -0.296971 -0.375203
singular values:
3-element Vector{Float64}:
12.317062005786141
0.5148992632302947
0.15767781817449114
Vt factor:
3×3 Matrix{Float64}:
-0.371764 -0.922107 0.107289
-0.213631 -0.0274913 -0.976527
0.903412 -0.385958 -0.18677
More matrix operations from linear algebra can be found in Linear Algebra.
{"id": "48c15860-736b-469d-8415-1bc38b8405b4", "data": [{"xaxis": "x", "mode": "lines", "x": [1, 2, 3, 4, 5, 6, 7, 8, 9], "showlegend": true, "name": "y1", "colorbar": {"title": {"text": ""}}, "zmin": null, "z": null, "legendgroup": "y1", "y": [3, 4, 5, 6, 8, 6, 5, 11, 12], "zmax": null, "line": {"shape": "linear", "color": "rgba(0, 154, 250, 1.000)", "dash": "solid", "width": 1}, "zaxis": null, "yaxis": "y", "metadata": {"smartZoomParams": {"maxCount": 9, "minCount": 25000, "currentCount": 9}, "shouldEnableSmartZoom": false}, "type": "scatter"}], "config": {"showlegend": true, "height": 400, "xaxis": {"showline": true, "showticklabels": true, "gridcolor": "rgba(0, 0, 0, 0.1)", "tickfont": {"family": "sans-serif", "color": "rgba(0, 0, 0, 1)", "size": 11}, "tickmode": "array", "anchor": "y", "range": [0.7599999999999998, 9.24], "tickcolor": "rgb(0, 0, 0)", "gridwidth": 0.5, "visible": true, "showgrid": true, "linecolor": "rgba(0, 0, 0, 1)", "zerolinecolor": "rgba(0, 0, 0, 1)", "tickangle": 0, "zeroline": false, "mirror": false, "domain": [0.03619130941965587, 0.9934383202099738], "title": {"font": {"family": "sans-serif", "color": "rgba(0, 0, 0, 1)", "size": 15}, "text": ""}, "ticks": "inside", "type": "linear"}, "annotations": [], "plot_bgcolor": "rgba(255, 255, 255, 1.000)", "paper_bgcolor": "rgba(255, 255, 255, 1.000)", "margin": {"t": 20, "b": 20, "r": 0, "l": 0}, "yaxis": {"showline": true, "showticklabels": true, "gridcolor": "rgba(0, 0, 0, 0.1)", "tickfont": {"family": "sans-serif", "color": "rgba(0, 0, 0, 1)", "size": 11}, "tickmode": "array", "anchor": "x", "range": [2.7299999999999995, 12.27], "tickcolor": "rgb(0, 0, 0)", "gridwidth": 0.5, "visible": true, "showgrid": true, "linecolor": "rgba(0, 0, 0, 1)", "zerolinecolor": "rgba(0, 0, 0, 1)", "tickangle": 0, "zeroline": false, "mirror": false, "domain": [0.03762029746281716, 0.9901574803149606], "title": {"font": {"family": "sans-serif", "color": "rgba(0, 0, 0, 1)", "size": 15}, "text": ""}, "ticks": "inside", "type": "linear"}, "width": 1515.828125, "legend": {"x": 1, "traceorder": "normal", "yanchor": "auto", "font": {"family": "sans-serif", "color": "rgba(0, 0, 0, 1)", "size": 11}, "borderwidth": 1, "bordercolor": "rgba(0, 0, 0, 1)", "y": 1, "bgcolor": "rgba(255, 255, 255, 1.000)", "tracegroupgap": 0, "title": {"font": {"family": "sans-serif", "color": "rgba(0, 0, 0, 1)", "size": 15}, "text": ""}, "xanchor": "auto"}}}
{"id": "5606a0b1-0fec-45bc-985a-c3a03f8bacd5", "data": [{"xaxis": "x", "mode": "lines", "x": [0.6, 0.6, 1.4, 1.4, 0.6, 0.6], "showlegend": true, "name": "y1", "fill": "tozeroy", "z": null, "legendgroup": "y1", "y": [3, 0, 0, 3, 3, 3], "line": {"color": "rgba(0, 0, 0, 1)", "dash": "solid", "width": 1}, "zaxis": null, "fillcolor": "rgba(0, 154, 250, 1.000)", "yaxis": "y", "metadata": {"smartZoomParams": {"maxCount": 6, "minCount": 25000, "currentCount": 6}, "shouldEnableSmartZoom": false}, "type": "scatter"}, {"xaxis": "x", "mode": "lines", "x": [1.6, 1.6, 2.4, 2.4, 1.6, 1.6], "showlegend": false, "name": "y1", "fill": "tozeroy", "z": null, "legendgroup": "y1", "y": [4, 0, 0, 4, 4, 4], "line": {"color": "rgba(0, 0, 0, 1)", "dash": "solid", "width": 1}, "zaxis": null, "fillcolor": "rgba(0, 154, 250, 1.000)", "yaxis": "y", "metadata": {"smartZoomParams": {"maxCount": 6, "minCount": 25000, "currentCount": 6}, "shouldEnableSmartZoom": false}, "type": "scatter"}, {"xaxis": "x", "mode": "lines", "x": [2.6, 2.6, 3.4, 3.4, 2.6, 2.6], "showlegend": false, "name": "y1", "fill": "tozeroy", "z": null, "legendgroup": "y1", "y": [5, 0, 0, 5, 5, 5], "line": {"color": "rgba(0, 0, 0, 1)", "dash": "solid", "width": 1}, "zaxis": null, "fillcolor": "rgba(0, 154, 250, 1.000)", "yaxis": "y", "metadata": {"smartZoomParams": {"maxCount": 6, "minCount": 25000, "currentCount": 6}, "shouldEnableSmartZoom": false}, "type": "scatter"}, {"xaxis": "x", "mode": "lines", "x": [3.6, 3.6, 4.4, 4.4, 3.6, 3.6], "showlegend": false, "name": "y1", "fill": "tozeroy", "z": null, "legendgroup": "y1", "y": [6, 0, 0, 6, 6, 6], "line": {"color": "rgba(0, 0, 0, 1)", "dash": "solid", "width": 1}, "zaxis": null, "fillcolor": "rgba(0, 154, 250, 1.000)", "yaxis": "y", "metadata": {"smartZoomParams": {"maxCount": 6, "minCount": 25000, "currentCount": 6}, "shouldEnableSmartZoom": false}, "type": "scatter"}, {"xaxis": "x", "mode": "lines", "x": [4.6, 4.6, 5.4, 5.4, 4.6, 4.6], "showlegend": false, "name": "y1", "fill": "tozeroy", "z": null, "legendgroup": "y1", "y": [8, 0, 0, 8, 8, 8], "line": {"color": "rgba(0, 0, 0, 1)", "dash": "solid", "width": 1}, "zaxis": null, "fillcolor": "rgba(0, 154, 250, 1.000)", "yaxis": "y", "metadata": {"smartZoomParams": {"maxCount": 6, "minCount": 25000, "currentCount": 6}, "shouldEnableSmartZoom": false}, "type": "scatter"}, {"xaxis": "x", "mode": "lines", "x": [5.6, 5.6, 6.4, 6.4, 5.6, 5.6], "showlegend": false, "name": "y1", "fill": "tozeroy", "z": null, "legendgroup": "y1", "y": [6, 0, 0, 6, 6, 6], "line": {"color": "rgba(0, 0, 0, 1)", "dash": "solid", "width": 1}, "zaxis": null, "fillcolor": "rgba(0, 154, 250, 1.000)", "yaxis": "y", "metadata": {"smartZoomParams": {"maxCount": 6, "minCount": 25000, "currentCount": 6}, "shouldEnableSmartZoom": false}, "type": "scatter"}, {"xaxis": "x", "mode": "lines", "x": [6.6, 6.6, 7.4, 7.4, 6.6, 6.6], "showlegend": false, "name": "y1", "fill": "tozeroy", "z": null, "legendgroup": "y1", "y": [5, 0, 0, 5, 5, 5], "line": {"color": "rgba(0, 0, 0, 1)", "dash": "solid", "width": 1}, "zaxis": null, "fillcolor": "rgba(0, 154, 250, 1.000)", "yaxis": "y", "metadata": {"smartZoomParams": {"maxCount": 6, "minCount": 25000, "currentCount": 6}, "shouldEnableSmartZoom": false}, "type": "scatter"}, {"xaxis": "x", "mode": "lines", "x": [7.6, 7.6, 8.4, 8.4, 7.6, 7.6], "showlegend": false, "name": "y1", "fill": "tozeroy", "z": null, "legendgroup": "y1", "y": [11, 0, 0, 11, 11, 11], "line": {"color": "rgba(0, 0, 0, 1)", "dash": "solid", "width": 1}, "zaxis": null, "fillcolor": "rgba(0, 154, 250, 1.000)", "yaxis": "y", "metadata": {"smartZoomParams": {"maxCount": 6, "minCount": 25000, "currentCount": 6}, "shouldEnableSmartZoom": false}, "type": "scatter"}, {"xaxis": "x", "mode": "lines", "x": [8.6, 8.6, 9.4, 9.4, 8.6, 8.6], "showlegend": false, "name": "y1", "fill": "tozeroy", "z": null, "legendgroup": "y1", "y": [12, 0, 0, 12, 12, 12], "line": {"color": "rgba(0, 0, 0, 1)", "dash": "solid", "width": 1}, "zaxis": null, "fillcolor": "rgba(0, 154, 250, 1.000)", "yaxis": "y", "metadata": {"smartZoomParams": {"maxCount": 6, "minCount": 25000, "currentCount": 6}, "shouldEnableSmartZoom": false}, "type": "scatter"}, {"xaxis": "x", "mode": "markers", "x": [1, 2, 3, 4, 5, 6, 7, 8, 9], "showlegend": false, "name": "y1", "colorbar": {"title": {"text": ""}}, "zmin": null, "z": null, "legendgroup": "y1", "y": [3, 4, 5, 6, 8, 6, 5, 11, 12], "zmax": null, "zaxis": null, "yaxis": "y", "marker": {"line": {"color": "rgba(0, 0, 0, 0)", "width": 1}, "symbol": "circle", "color": "rgba(0, 154, 250, 0.000)", "size": 0}, "metadata": {"smartZoomParams": {"maxCount": 9, "minCount": 25000, "currentCount": 9}, "shouldEnableSmartZoom": false}, "type": "scatter"}], "config": {"showlegend": true, "height": 400, "xaxis": {"showline": true, "showticklabels": true, "gridcolor": "rgba(0, 0, 0, 0.1)", "tickfont": {"family": "sans-serif", "color": "rgba(0, 0, 0, 1)", "size": 11}, "tickmode": "array", "anchor": "y", "range": [0.05615999999999843, 9.943840000000002], "tickcolor": "rgb(0, 0, 0)", "gridwidth": 0.5, "visible": true, "showgrid": true, "linecolor": "rgba(0, 0, 0, 1)", "zerolinecolor": "rgba(0, 0, 0, 1)", "tickangle": 0, "zeroline": false, "mirror": false, "domain": [0.09128390201224845, 0.9934383202099738], "title": {"font": {"family": "sans-serif", "color": "rgba(0, 0, 0, 1)", "size": 15}, "text": "месяц"}, "ticks": "inside", "type": "linear"}, "annotations": [], "plot_bgcolor": "rgba(255, 255, 255, 1.000)", "paper_bgcolor": "rgba(255, 255, 255, 1.000)", "margin": {"t": 20, "b": 20, "r": 0, "l": 0}, "yaxis": {"showline": true, "showticklabels": true, "gridcolor": "rgba(0, 0, 0, 0.1)", "tickfont": {"family": "sans-serif", "color": "rgba(0, 0, 0, 1)", "size": 11}, "tickmode": "array", "anchor": "x", "range": [-0.3600000000000003, 12.36], "tickcolor": "rgb(0, 0, 0)", "gridwidth": 0.5, "visible": true, "showgrid": true, "linecolor": "rgba(0, 0, 0, 1)", "zerolinecolor": "rgba(0, 0, 0, 1)", "tickangle": 0, "zeroline": false, "mirror": false, "domain": [0.07581474190726165, 0.9901574803149606], "title": {"font": {"family": "sans-serif", "color": "rgba(0, 0, 0, 1)", "size": 15}, "text": "количество"}, "ticks": "inside", "type": "linear"}, "width": 1515.828125, "legend": {"x": 1, "traceorder": "normal", "yanchor": "auto", "font": {"family": "sans-serif", "color": "rgba(0, 0, 0, 1)", "size": 11}, "borderwidth": 1, "bordercolor": "rgba(0, 0, 0, 1)", "y": 1, "bgcolor": "rgba(255, 255, 255, 1.000)", "tracegroupgap": 0, "title": {"font": {"family": "sans-serif", "color": "rgba(0, 0, 0, 1)", "size": 15}, "text": ""}, "xanchor": "auto"}}}