MATLABMATLAB · Chapterبەش 4 · Lectureوانە 4.5
Matrix Operations vs Element-by-Element Operationsکارەکانی ماتریکس بەرامبەر کارەکانی توخم بە توخم
Add and subtract arrays of the same size, multiply matrices with * (columns of A = rows of B), use the dotted operators .* ./ .\ .^ to pair up elements (same or compatible sizes), tell the matrix power A^2 from A.^2, see why formulas on vectors need the dot, and solve with A\b.
هەمان قەبارە کۆ بکەرەوە و لێدەربکە، بە * لێک بدە (ستوونەکانی A = ڕیزەکانی B)، .* ./ .\ .^ بۆ جووتکردنی بەکار بهێنە (قەبارەی یەکسان یان گونجاو)، A^2 لە A.^2 جیا بکەرەوە، ببینە بۆچی لەسەر پێویستیان بە خاڵە و بە A\b چارەسەر بکە.
What you will learnچی فێر دەبیت
- Add, subtract and scale arrays and explain the same-size rule.
- Multiply matrices with
*and predict the size of the result or the error. - Use
.*,./,.\and.^on arrays of the same or compatible sizes. - Tell
A^2(matrix power) fromA.^2and write formulas on vectors with the dot. - Solve a system of linear equations with
A\b.
- کۆ بکەیتەوە، لێدەربکەیت و لە ژمارە بدەیت و یاسای هەمان قەبارە ڕوون بکەیتەوە.
- بە
*لێک بدەیت و قەبارەی ئەنجامەکە یان پێشبینی بکەیت. .*،./،.\و.^لەسەر قەبارەی یەکسان یان گونجاو بەکار بهێنیت.A^2( ) لەA.^2جیا بکەیتەوە و لەسەر بە خاڵ بنووسیت.- سیستەمێکی بە
A\bچارەسەر بکەیت.
Adding and subtracting arraysکۆکردنەوە و لێدەرکردنی ڕیزەکەکان
+ and - work element by element: the first element with the first, the second with the second, and so on. So both arrays must have the same size — exactly the rule of matrix addition in maths (the same-size rule). Every example in this lesson also runs in GNU Octave and in the free version of MATLAB Online.
+ و - توخم بە توخم کار دەکەن: یەکەم لەگەڵ یەکەم، دووەم لەگەڵ دووەم و هتد. بۆیە هەردوو دەبێت هەمان قەبارەیان هەبێت — ڕێک یاسای کۆکردنەوەی لە ماتماتیکدا (یاسای هەمان قەبارە). هەموو نموونەیەکی ئەم وانەیە لە GNU Octave و لە وەشانی بەخۆڕایی MATLAB Onlineـیشدا کار دەکات.
a = [6 2 9];
b = [4 7 1];
a + b
a - b
P = [4 -2 7; 3 6 -1];
Q = [1 5 -3; 8 -4 2];
P + Q
P - Qans =
10 9 10
ans =
2 -5 8
ans =
5 3 4
11 2 1
ans =
3 -7 10
-5 10 -3Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 3
, , .
، ، .
- Lineدێڕی 8
Position by position: , , , …
شوێن بە شوێن: ، ، ، …
Adding and subtracting arraysکۆکردنەوە و لێدەرکردنی ڕیزەکەکان
A number with an arrayژمارەیەک لەگەڵ ڕیزەکێک
A single number (a ) is different: it goes to every element. P + 10 adds 10 to all six elements, 2*P doubles them and P/2 halves them. No dot is needed for these (multiplying a matrix by a number).
تەنها یەک ژمارە () جیاوازە: دەچێتە سەر هەموو . P + 10 10 بۆ هەر شەش زیاد دەکات، 2*P دووجایان دەکات و P/2 نیوەیان دەکات. بۆ ئەمانە خاڵ پێویست نییە (لێکدانی ماتریکسێک لە ژمارەیەک).
P + 10
2*P
P/2ans =
14 8 17
13 16 9
ans =
8 -4 14
6 12 -2
ans =
2.0000 -1.0000 3.5000
1.5000 3.0000 -0.5000Matrix multiplication: *لێکدانی ماتریکسەکان: *
* between two matrices is matrix multiplication from : each entry of the answer is a row of the first matrix times a column of the second — multiply in pairs, then add (row times column, one entry). So the number of columns of A must equal the number of rows of B, and the answer has the rows of A and the columns of B: a times a gives a .
* لە نێوان دوو لێکدانی لە : هەر ژمارەیەکی وەڵامەکە ڕیزێکی یەکەم جارانی ستوونێکی دووەمە — جووت جووت لێکیان بدە، پاشان کۆیان بکەرەوە (ڕیز جارانی ستوون، یەک ژمارە). بۆیە ژمارەی ستوونەکانی A دەبێت یەکسان بێت بە ژمارەی ڕیزەکانی B و وەڵامەکە ڕیزەکانی A و ستوونەکانی Bـی هەیە: جارانی دەکاتە .
![Animation: A = [2 1 3; 0 4 1; 5 2 2] times B = [1 3; 2 0; 4 1]. In each frame one row of A and one column of B are highlighted and one entry of the 3 × 2 answer is written: 2·1 + 1·2 + 3·4 = 16, then 9, 12, 1, 17 and 17. The last frame shows C = [16 9; 12 1; 17 17].](/_astro/matlab-array-math-product-cells.en.Bt5CUcQi.gif)
![Animation: A = [2 1 3; 0 4 1; 5 2 2] times B = [1 3; 2 0; 4 1]. In each frame one row of A and one column of B are highlighted and one entry of the 3 × 2 answer is written: 2·1 + 1·2 + 3·4 = 16, then 9, 12, 1, 17 and 17. The last frame shows C = [16 9; 12 1; 17 17].](/_astro/matlab-array-math-product-cells.ckb.DD8iUrNI.gif)
A = [2 1 3; 0 4 1; 5 2 2];
B = [1 3; 2 0; 4 1];
C = A*B
F = [2 0 1; 1 3 2; 4 1 0; 0 2 5];
G = [3 1; 2 4; 1 0];
F*GC =
16 9
12 1
17 17
ans =
7 2
11 13
14 8
9 8Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 3
times : the inner sizes (3 and 3) agree, the answer is . Entry (1,1): .
جارانی : قەبارە ناوەکییەکان (3 و 3) یەک دەگرنەوە، وەڵامەکە ـە. ژمارەی (1,1): .
- Lineدێڕی 6
times gives . Entry (2,2): .
جارانی دەکاتە . ژمارەی (2,2): .
Matrix multiplication: *لێکدانی ماتریکسەکان: *
When * fails, and why order mattersکەی * سەرناکەوێت و بۆچی ڕیزبەندی گرنگە
Swap the two matrices above and the inner sizes no longer agree: G*F is a times a , and . MATLAB stops with an about incorrect dimensions for matrix multiplication (older releases, like the ones in the course notes, said “Inner matrix dimensions must agree.”). Even when both orders work, the answers usually differ: matrix multiplication is not commutative (AB and BA).
ئەگەر دوو سەرەوە بگۆڕیتەوە، قەبارە ناوەکییەکان ئیتر یەک ناگرنەوە: G*F جارانی ـە و . MATLAB بە دەربارەی ڕەهەندی هەڵە بۆ لێکدانی دەوەستێت (وەشانە کۆنەکان، وەک ئەوانەی تێبینییەکانی وانەکە، دەیانگوت «Inner matrix dimensions must agree.»). تەنانەت کاتێک هەردوو ڕیزبەندییەکە کار دەکەن، وەڵامەکان زۆربەی جار جیاوازن: لێکدانی گۆڕینەوەیی نییە، واتە ڕیزبەندی گرنگە (AB و BA).
G*F
U = [2 1; 0 3];
V = [1 4; 2 1];
U*V
V*Uans =
4 9
6 3
ans =
2 13
4 5Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 1
An error: 2 columns of G against 4 rows of F. No answer is printed.
: 2 ستوونی G بەرامبەر 4 ڕیزی F. هیچ وەڵامێک نانووسرێت.
- Lineدێڕی 4-5
Both orders exist for matrices, but .
بۆ هەردوو ڕیزبەندی هەن، بەڵام .
A MATLAB beginner course by Phil Parisi: the Command Window, variables, vectors and matrices, indexing, scripts, sections and naming rules, each in short parts.
کۆرسێکی سەرەتایی MATLAB لەلایەن Phil Parisi: Command Window، ، و ، ، ، بەشەکان و یاساکانی ناونان، هەر یەکەیان بە بەشێکی کورت.
Watch 19:30-22:24: a number goes to every element, AA fails because the inner sizes differ, A'A works, and x.^2 squares each element.
لە 19:30 تا 22:24 سەیر بکە: ژمارەیەک بۆ هەموو دەچێت، A*A سەرناکەوێت چونکە قەبارە ناوەکییەکان جیاوازن، A'*A کار دەکات و x.^2 هەر دووجا دەکات.
Matrix multiplication: *لێکدانی ماتریکسەکان: *
A row times a column: one numberڕیز جارانی ستوون: یەک ژمارە
A row times an column is a matrix — a single number. This is the of the two vectors, also given by dot. The other order, column times row, gives a whole table.
ڕیزێکی جارانی ستوونێکی ـە — تەنها یەک ژمارە. ئەمە دوو کە dotـیش دەیدات. ڕیزبەندییەکەی تر، ستوون جارانی ڕیز، خشتەیەکی تەواوی دەدات.
r = [3 1 2];
c = [4; 5; 1];
r*c
dot(r, c)
c*rans =
19
ans =
19
ans =
12 4 8
15 5 10
3 1 2Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 3
.
یەک ژمارە: .
- Lineدێڕی 5
times : a table of all the products.
جارانی : خشتەیەکی لە هەموو لێکدانەکان.
Element by element: .*, ./, .\ , .^توخم بە توخم: .*، ./، .\ ، .^
Often you do not want linear algebra at all — you just want to multiply each price by its own quantity. Put a dot before the operator and MATLAB pairs up the elements in the same position, like + does. This is an : A.*B multiplies, A./B divides (A over B), A.\B divides the other way (B over A) and A.^n raises every element to the power n.
زۆر جار پێویستت بە نییە — تەنها دەتەوێت هەر نرخێک لە بڕی خۆی بدەیت. خاڵێک بخە پێش و MATLAB لە هەمان شوێندا جووت دەکات، وەک +. ئەمە : A.*B لێک دەدات، A./B دابەش دەکات (A بەسەر B)، A.\B بە پێچەوانەوە دابەش دەکات (B بەسەر A) و A.^n هەموو بەرز دەکاتەوە بۆ n.
* (row × column) and .* (cell × cell)هەمان دوو ، دوو لێکدانی جیاواز: * (ڕیز × ستوون) و .* (خانە × خانە)CêITA = [1 2 3; 4 5 6; 7 8 9];
B = [1 1 1; 2 2 2; 3 3 3];
A./B
A.*B
B.\Aans =
1.0000 2.0000 3.0000
2.0000 2.5000 3.0000
2.3333 2.6667 3.0000
ans =
1 2 3
8 10 12
21 24 27
ans =
1.0000 2.0000 3.0000
2.0000 2.5000 3.0000
2.3333 2.6667 3.0000Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 3
Each element of A over the element of B in the same place: , , …
هەر A بەسەر B لە هەمان شوێن: ، ، …
- Lineدێڕی 4
Each pair multiplied: row 2 of A is doubled, row 3 tripled.
هەر جووتێک لێک دراوە: ڕیزی 2ی A دوو جار، ڕیزی 3 سێ جار.
- Lineدێڕی 5
B.\Ais “B under A”, the same asA./B.B.\Aواتە «B لە ژێر A»، هەمانA./B.
Element by element: .*, ./, .\ , .^توخم بە توخم: .*، ./، .\ ، .^
Compatible sizes: a row with a columnقەبارەی گونجاو: ڕیزێک لەگەڵ ستوونێک
Element-wise operators also accept arrays whose sizes are compatible: in each dimension the two sizes are equal, or one of them is 1. The size-1 array is then repeated to fit. A is the simplest case. The useful one is a column with a row: an column and a row give a whole table. This rule is called (MATLAB R2016b and newer); Octave calls it broadcasting and does the same (its error for sizes that do not fit speaks of “nonconformant arguments”). Sizes that are not compatible stop with the message “Arrays have incompatible sizes for this operation.”
بە قەبارەی گونجاویش وەردەگرن: لە هەر ڕەهەندێکدا دوو قەبارەکە یەکسانن، یان یەکێکیان 1ـە. پاشان ئەو قەبارەکەی 1ـە دووبارە دەکرێتەوە تا جێ ببێتەوە. سادەترین بارە. بارە بەسوودەکە ستوونێکە لەگەڵ ڕیزێک: ستوونێکی و ڕیزێکی خشتەیەکی تەواوی دەدەن. ئەم یاسایە پێی دەگوترێت (MATLAB R2016b و نوێتر)؛ Octave پێی دەڵێت broadcasting و هەمان کار دەکات ( بۆ قەبارەی ناگونجاو باسی «nonconformant arguments» دەکات). قەبارەی ناگونجاو بەم دەوەستێت: «Arrays have incompatible sizes for this operation.»
t = (1:5)' .* (1:5)
[1 2 3] + [10 20]t =
1 2 3 4 5
2 4 6 8 10
3 6 9 12 15
4 8 12 16 20
5 10 15 20 25Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 1
A column times a row, element by element: the times table of linear algebra and matrices.
ستوونێکی جارانی ڕیزێکی ، توخم بە توخم: خشتەی لێکدانی ـی جەبری هێڵی و ماتریکسەکان.
- Lineدێڕی 2
and : the second dimension is 3 against 2, neither is 1 — incompatible, so MATLAB stops with an error.
و : ڕەهەندی دووەم 3 بەرامبەر 2ـە، هیچیان 1 نییە — ناگونجاوە، بۆیە MATLAB بە دەوەستێت.
A^2 or A.^2?A^2 یان A.^2؟
A^2 means A*A: the matrix power, built from row × column products, so A must be square (powers of a square matrix). A.^2 squares every element on its own. For the matrix A above they are very different: entry (1,1) of A^2 is row 1 of A times column 1 of A, , while entry (1,1) of A.^2 is just .
A^2 واتە A*A: کە لە لێکدانی ڕیز × ستوون دروست دەبێت، بۆیە A دەبێت چوارگۆشەیی بێت (توانی ماتریکسێکی چوارگۆشەیی). A.^2 هەموو بە تەنها دووجا دەکات. بۆ Aـی سەرەوە زۆر جیاوازن: ژمارەی (1,1)ی A^2 ڕیزی 1ی A جارانی ستوونی 1ی Aـیە، ، لە کاتێکدا ژمارەی (1,1)ی A.^2 تەنها ـە.
A^2: row 1 times column 1 = 30ژمارەی (1,1)ی A^2: ڕیزی 1 جارانی ستوونی 1 = 30CêITA^2
A.^2ans =
30 36 42
66 81 96
102 126 150
ans =
1 4 9
16 25 36
49 64 81Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 1
The matrix power,
A*A.،
A*A.- Lineدێڕی 2
Every element squared.
هەموو دووجا کراوە.
Why the dot mattersبۆچی خاڵەکە گرنگە
In real programs you mostly use vectors of values — times, prices, radii — and want a formula applied to each value. Then the dot is essential. To compute for you write x.^2. Without the dot, x^2 asks for the matrix power of a row, which is not square, so MATLAB stops with an error; x*y for two rows fails the same way ( times ).
لە بەرنامەی ڕاستەقینەدا زۆرتر ژمارە بەکار دەهێنیت — کات، نرخ، نیوەتیرە — و دەتەوێت لەسەر هەر جێبەجێ بکرێت. ئەو کاتە خاڵەکە پێویستە. بۆ هەژمارکردنی بۆ دەنووسیت x.^2. بێ خاڵ، x^2 داوای ڕیزێکی دەکات کە چوارگۆشەیی نییە، بۆیە MATLAB بە دەوەستێت؛ x*y بۆ دوو ڕیز بە هەمان شێوە سەرناکەوێت ( جارانی ).
x = 0:0.5:2;
y = x.^2
r = [1 2 3];
area = pi*r.^2
x^2y =
0 0.2500 1.0000 2.2500 4.0000
area =
3.1416 12.5664 28.2743Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 2
Each x squared.
هەر x دووجا کراوە.
- Lineدێڕی 4
Areas of three circles:
pi*needs no dot (a number),r.^2does.ڕووبەری سێ بازنە:
pi*خاڵی پێویست نییە (ژمارەیە)،r.^2پێویستی هەیە.- Lineدێڕی 5
An error:
^needs a square matrix. Use.^for the power of each element.:
^پێویستی بە هەیە. بۆ هەر.^بەکار بهێنە.
| Matrix operator (linear algebra) () | Element-wise operator توخم بە توخم | Needsپێویستی بە |
|---|---|---|
A*B | A.*B | *: columns of A = rows of B; .*: same or compatible sizes*: ستوونەکانی A = ڕیزەکانی B؛ .*: هەمان قەبارە یان قەبارەی گونجاو |
A/B, A\B | A./B, A.\B | / \ solve equations (next screen); ./ .\ divide pair by pair/ \ چارەسەر دەکەن (شاشەی دواتر)؛ ./ .\ جووت بە جووت دابەش دەکەن |
A^n | A.^n | ^: square A; .^: any A^: Aـی چوارگۆشەیی؛ .^: هەر A |
A' | A.' | both swap rows and columns; ' also conjugates complex numbers (the transpose)هەردووکیان ڕیز و ستوون دەگۆڕنەوە؛ ' بۆ وەردەگرێت (جێگۆڕکێی ماتریکس) |
A+B, A-B | the sameهەمان شت | already element by element; no dot exists or is neededخۆیان توخم بە توخمن؛ خاڵ نییە و پێویستیش نییە |
2*A, A/2 | 2.*A, A./2 | with a single number both forms give the same answerلەگەڵ تەنها یەک ژمارە هەردوو شێوەکە هەمان وەڵام دەدەن |
Left division solves equations: A\bدابەشکردنی چەپ هاوکێشەکان چارەسەر دەکات: A\b
You met \ on numbers: 5\3 means 3 divided by 5 (the arithmetic operators). On matrices, x = A\b solves the system of : it finds the column x that A turns into b. In maths the answer is (solving with the inverse), and inv(A)*b gives the same numbers — but MathWorks recommends A\b: it solves by row operations without building the , so it is faster and more accurate.
پێشتر \ ـت لەسەر ژمارە بینی: 5\3 واتە 3 دابەش بە 5 (کارپێکەرە ژمێرەییەکان). لەسەر ، x = A\b سیستەمی چارەسەر دەکات: ئەو ستوونە x دەدۆزێتەوە کە A دەیکات بە b. لە ماتماتیکدا وەڵامەکە ـە (چارەسەرکردنی بە پێچەوانەی ماتریکس)، inv(A)*bـیش هەمان ژمارەکان دەدات — بەڵام MathWorks A\b پێشنیار دەکات: بە چارەسەر دەکات بێ دروستکردنی ، بۆیە خێراتر و وردترە.
At a tea house Aram pays 2500 IQD for 4 teas and 2 sandwiches; Shilan pays 2500 IQD for 1 tea and 3 sandwiches. x is the price of a tea, y the price of a sandwich.
لە چایخانەیەک ئارام 2500 دینار دەدات بۆ 4 چا و 2 ساندویچ؛ شیلان 2500 دینار دەدات بۆ 1 چا و 3 ساندویچ. x نرخی چایەکە، y نرخی ساندویچێک.
A = [4 2; 1 3];
b = [2500; 2500];
x = A\bx = 250 750
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 3
A tea costs 250 IQD and a sandwich 750 IQD. Check:
A*xgives backb.inv(A)*bgives the same answer here.چایەک 250 دینار و ساندویچێک 750 دینار. پشکنین:
A*xbدەگەڕێنێتەوە.inv(A)*bـیش لێرەدا هەمان وەڵام دەدات.
If A is a (no inverse), the system has no single answer and MATLAB warns (a determinant that should be 0). For more than two equations nothing changes: A is and b has n rows.
ئەگەر A بێت (پێچەوانەی نەبێت)، سیستەمەکە یەک وەڵامی نییە و MATLAB ئاگادار دەکاتەوە (دیتەرمینانتێک کە دەبوو 0 بێت). بۆ زیاتر لە دوو هیچ ناگۆڕێت: A n × nـە و b n ڕیزی هەیە.
How a system of linear equations becomes A*x = b, and how Ab solves it in one step.
چۆن چەند پێکەوە بە شێوەی A*x = b دەنووسرێنەوە و چۆن A\b بە یەک هەنگاو چارەسەریان دەکات.
Watch 42:20-47:06: a system of three equations from Wikipedia written as A and b, solved first with inv(A)*b and then with Ab; both give 1, -2, -2.
لە 42:20 تا 47:06 سەیر بکە: سێ لە Wikipediaـەوە کە وەک A و b دەنووسرێن، سەرەتا بە inv(A)*b و پاشان بە A\b چارەسەر دەکرێن؛ هەردووکیان 1, -2, -2 دەدەن.
Mixed practiceڕاهێنانی تێکەڵ
The Kurdish guide in the course material practises all of these operations on small matrices. Here is the same kind of practice with new matrices. Work out each answer on paper first, then check in MATLAB.
ڕێبەرە کوردییەکەی ناو کەرەستەکانی وانەکە هەموو ئەم کارانە لەسەر بچووک ڕاهێنان پێ دەکات. ئەمە هەمان جۆری ڕاهێنانە بە نوێ. سەرەتا وەڵامی هەر یەکێک لەسەر کاغەز دەربهێنە، پاشان لە MATLABـدا بیپشکنە.
Let C = [2 4; 6 8] and D = [1 3; 5 7]. Find C.*D, C*D, C/2, C.^2, C^2 and C./D.
با C = [2 4; 6 8] و D = [1 3; 5 7] بن. C.*D، C*D، C/2، C.^2، C^2 و C./D بدۆزەرەوە.
Show answerوەڵامەکە پیشان بدە
C = [2 4; 6 8];
D = [1 3; 5 7];
C.*D
C*D
C/2
C.^2
C^2
C./Dans =
2 12
30 56
ans =
22 34
46 74
ans =
1 2
3 4
ans =
4 16
36 64
ans =
28 40
60 88
ans =
2.0000 1.3333
1.2000 1.1429Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 3-4
Element-wise: , , , . Matrix product: entry (1,1) is .
توخم بە توخم: ، ، ، . لێکدانی : ژمارەی (1,1): .
- Lineدێڕی 6-7
Each element squared, against
C*C: entry (1,1) is .هەر دووجا کراوە، بەرامبەر
C*C: ژمارەی (1,1): .
Adding and deleting rows and columns, which the guide practises on the same pages, is in indexing.
زیادکردن و سڕینەوەی ڕیز و ستوون، کە ڕێبەرەکە لە هەمان پەڕەکاندا ڕاهێنانی پێ دەکات، لە ئیندێکسەکاندایە.
Common mistakesهەڵە باوەکان
| Mistakeهەڵە | What happensچی ڕوودەدات | Fixچارەسەر |
|---|---|---|
Forgetting the dot: x^2, x*y, x/y on two vectorsلەبیرچوونی خاڵ: x^2، x*y، x/y لەسەر دوو | Errors — and x/y even gives one strange number with no error — x/y تەنانەت یەک ژمارەی سەیر بێ هیچ دەدات | x.^2, x.*y, x./yx.^2، x.*y، x./y |
Using .* when the maths needs a matrix productبەکارهێنانی .* کاتێک ماتماتیکەکە لێکدانی دەوێت | A wrong answer with no error (for equal sizes)وەڵامێکی هەڵە بێ (بۆ قەبارەی یەکسان) | Ask “row times column, or cell times cell?” before you type.پێش نووسین بپرسە: «ڕیز جارانی ستوون، یان خانە جارانی خانە؟» |
Expecting A^2 to square each elementچاوەڕوانیی ئەوەی A^2 هەموو دووجا بکات | You get A*AA*A وەردەگریت | A.^2A.^2 |
Expecting A*B to equal B*Aچاوەڕوانیی ئەوەی A*B یەکسان بێت بە B*A | Different answers, or an error for one orderوەڵامی جیاواز، یان بۆ یەکێک لە ڕیزبەندییەکان | Keep the order of the maths.ڕیزبەندیی ماتماتیکەکە بپارێزە. |
Adding arrays of different sizes: [1 2 3] + [4 5]کۆکردنەوەی قەبارە جیاواز: [1 2 3] + [4 5] | “Arrays have incompatible sizes for this operation.”ئەم : «Arrays have incompatible sizes for this operation.» | Same size, or a row with a column on purpose.هەمان قەبارە، یان بە ئەنقەست ڕیزێک لەگەڵ ستوونێک. |
inv(A)*b to solve equationsinv(A)*b بۆ چارەسەرکردنی | Works, but slower and less accurateکار دەکات، بەڵام هێواشتر و کەمتر ورد | A\bA\b |
Copying >>> or capital names (Ans, Sum) from old screenshotsکۆپیکردنی >>> یان ناوی بە پیتی گەورە (Ans، Sum) لە وێنە کۆنەکان | >>> is Python's prompt; capitals are different names>>> هی Pythonـە؛ پیتی گەورە ناوی جیاوازن | MATLAB's prompt is >>; write ans, sum.لە MATLABـدا >>ـە؛ بنووسە ans، sum. |
If you know Python: [1, 2] * 2 there repeats the list to [1, 2, 1, 2] instead of doubling the numbers (repeating a list). In MATLAB [1 2]*2 is [2 4].
ئەگەر Python دەزانیت: لەوێ [1, 2] * 2 لیستەکە دووبارە دەکاتەوە بۆ [1, 2, 1, 2] لە جیاتی دووجاکردنی ژمارەکان (دووبارەکردنەوەی لیست). لە MATLABـدا [1 2]*2 دەکاتە [2 4].
Practiceڕاهێنان
Dilan orders 2 teas, 1 coffee and 3 juices. Prices are 250, 750 and 1000 IQD. Store the quantities as a row q and the prices as a column p, and get the total with one multiplication.
دیلان 2 چا، 1 قاوە و 3 شەربەت داوا دەکات. نرخەکان 250، 750 و 1000 دینارن. بڕەکان وەک ڕیزی q و نرخەکان وەک ستوونی p هەڵبگرە و کۆی گشتی بە یەک لێکدان وەربگرە.
Show answerوەڵامەکە پیشان بدە
q = [2 1 3];
p = [250; 750; 1000];
total = q*ptotal =
4250Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 3
Row times column: IQD (a shopping bill in IQD).
ڕیز جارانی ستوون: دینار (پسوولەی کڕین بە دینار).
Soran wants for and types x = 0:4; y = x^2 + 3*x. What goes wrong, and what is the fix?
سۆران ـی دەوێت بۆ و دەنووسێت x = 0:4; y = x^2 + 3*x. چی هەڵە دەبێت و چارەسەرەکە چییە؟
Show answerوەڵامەکە پیشان بدە
x^2 asks for the matrix power of a row, which is not square: an error. 3*x is fine (a number times a vector).
x^2 داوای ڕیزێکی دەکات کە چوارگۆشەیی نییە: . 3*x باشە (ژمارەیەک جارانی ).
x = 0:4;
y = x.^2 + 3*xy =
0 4 10 18 28Predict the size of each result, or say that it is an error: (a) ones(2,3)*ones(3,4) (b) ones(2,3).*ones(2,3) (c) ones(2,3)*ones(2,3) (d) ones(3,1).*ones(1,4) (e) ones(1,5)*ones(5,1)
قەبارەی هەر ئەنجامێک پێشبینی بکە، یان بڵێ : (أ) ones(2,3)*ones(3,4) (ب) ones(2,3).*ones(2,3) (ج) ones(2,3)*ones(2,3) (د) ones(3,1).*ones(1,4) (هـ) ones(1,5)*ones(5,1)
Show answerوەڵامەکە پیشان بدە
- (a) — inner sizes 3 and 3 agree.
- (b) — same size, element by element.
- (c) An error — inner sizes 3 and 2 differ.
- (d) — a column with a row: compatible sizes.
- (e) — the number 5 (row times column).
- (أ) — قەبارە ناوەکییەکان 3 و 3 یەک دەگرنەوە.
- (ب) — هەمان قەبارە، توخم بە توخم.
- (ج) — قەبارە ناوەکییەکان 3 و 2 جیاوازن.
- (د) — ستوونێک لەگەڵ ڕیزێک: قەبارەی گونجاو.
- (هـ) — ژمارە 5 (ڕیز جارانی ستوون).
\ چارەسەری بکەSolve and with \ and check the answer.
و بە \ چارەسەر بکە و وەڵامەکە بپشکنە.
Show answerوەڵامەکە پیشان بدە
A = [2 1; 1 -1];
b = [7; -1];
x = A\b
A*xx =
2
3
ans =
7
-1Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 4
Putting the answer back gives b again: is right.
دانانەوەی وەڵامەکە b دەگەڕێنێتەوە: ڕاستە.
^ and .^ agreeکەی ^ و .^ یەک دەگرنەوەFor N = [2 0; 0 3], compare N^2 and N.^2. Why are they equal here, but not for most matrices?
بۆ N = [2 0; 0 3]، N^2 و N.^2 بەراورد بکە. بۆچی لێرەدا یەکسانن، بەڵام بۆ زۆربەی نا؟
Show answerوەڵامەکە پیشان بدە
N = [2 0; 0 3];
N^2
N.^2ans =
4 0
0 9
ans =
4 0
0 9N is a diagonal matrix: in each row × column product only one pair is not zero, so the matrix power squares the diagonal entries too. As soon as a matrix has nonzero entries off the diagonal, the two answers differ.
N تیرەییە: لە هەر لێکدانێکی ڕیز × ستووندا تەنها یەک جووت سفر نییە، بۆیە ژمارەکانی سەر تیرەکە دووجا دەکات. هەر کە ژمارەی ناسفری لە دەرەوەی تیرەکە هەبێت، دوو وەڵامەکە جیاواز دەبن.
Summaryپوختە
+and-work element by element on arrays of the same size; a single number goes to every element.A*Bis matrix multiplication: columns of A = rows of B; the result is rows(A) × columns(B); in generalA*Bis not equal toB*A; a row times a column is one number..*,./,.\,.^pair up the elements; the sizes must be the same or compatible (a row with a column gives a table).A^2=A*A(square A only);A.^2squares each element. Formulas on vectors need the dots:x.^2.x = A\bsolves ; prefer it toinv(A)*b.
+و-توخم بە توخم لەسەر هەمان قەبارە کار دەکەن؛ تەنها یەک ژمارە دەچێتە سەر هەموو .A*Bلێکدانی : ستوونەکانی A = ڕیزەکانی B؛ ئەنجامەکە ڕیزەکانی A × ستوونەکانی Bـیە؛ بە گشتیA*Bیەکسان نییە بەB*A؛ ڕیز جارانی ستوون یەک ژمارەیە..*،./،.\،.^جووت دەکەن؛ قەبارەکان دەبێت یەکسان یان گونجاو بن (ڕیزێک لەگەڵ ستوونێک خشتەیەک دەدات).A^2=A*A(تەنها Aـی چوارگۆشەیی)؛A.^2هەر دووجا دەکات. لەسەر پێویستیان بە خاڵەکانە:x.^2.x = A\bچارەسەر دەکات؛ لەinv(A)*bباشترە.
What is the difference between A*B and A.*B?جیاوازیی نێوان A*B و A.*B چییە؟
A*B multiplies rows by columns (linear algebra); A.*B multiplies elements in the same place.
A*B ڕیزەکان لە ستوونەکان دەدات ()؛ A.*B هەمان شوێن لێک دەدات.
Why does x^2 fail for x = 1:5?بۆچی x^2 بۆ x = 1:5 سەرناکەوێت؟
^ is the matrix power and needs a square matrix; use x.^2.
^ و پێویستی بە هەیە؛ x.^2 بەکار بهێنە.
Which sizes can be combined element by element?کام قەبارانە دەتوانن توخم بە توخم تێکەڵ بکرێن؟
Equal sizes, or sizes where each dimension is equal or 1 (a scalar, or a row with a column).
قەبارەی یەکسان، یان قەبارەیەک کە هەر ڕەهەندێکی یەکسان یان 1 بێت (، یان ڕیزێک لەگەڵ ستوونێک).
Videoڤیدیۆ
Watch a videoسەیری ڤیدیۆیەک بکە
A video that viewers liked a lot and praised in their comments, chosen for this lesson.ڤیدیۆیەک کە بینەران زۆریان پێ خۆش بووە و لە کۆمێنتەکانیاندا ستایشیان کردووە، بۆ ئەم وانەیە هەڵبژێردراوە.
Matrices in MATLAB with clear examples: typing a matrix, matrix versus element-wise multiplication, submatrices, transpose, joining, zeros/ones/eye/rand, linspace and size.
لە MATLABـدا بە نموونە: نووسینی ، لێکدانی بەرامبەر ، بەشێک لە ، ، ، zeros/ones/eye/rand، linspace و size.
Watch 6:59-13:08: AB as matrix multiplication (inner sizes must match), then . ./ and .^ for element-by-element work: time times speed gives distance, distance ./ time gives speed back.
لە 6:59 تا 13:08 سەیر بکە: A*B وەک لێکدانی (قەبارە ناوەکییەکان دەبێت وەک یەک بن)، پاشان .*، ./ و .^ بۆ : لێکدانی کات و خێرایی دووری دەدات و دووری بە کات (بە ./) خێرایی دەداتەوە.
Lesson complete!وانەکە تەواو بوو!
Well done. Review the key terms, then go on to the next lesson.دەستخۆش. سەیرێکی زاراوە سەرەکییەکان بکەرەوە، پاشان بڕۆ بۆ وانەی داهاتوو.
Key termsزاراوە سەرەکییەکان
Script Files, input, disp and fprintfفایلەکانی دەسنووس، input، disp و fprintf
Related lecturesوانە پەیوەندیدارەکان
- Creating Matricesدروستکردنی ماتریکسەکان
- Indexing, the Colon, Adding and Deleting Elementsئیندێکسەکان، دوو خاڵ، زیادکردن و سڕینەوەی توخمەکان
- Adding, Subtracting and Scaling Matricesکۆکردنەوە، لێدەرکردن و لێکدانی ماتریکسەکان لە ژمارە
- Multiplying Matricesلێکدانی ماتریکسەکان
- The Inverse of a Matrixپێچەوانەی ماتریکس
- Algebra, Boolean Logic and Matrices Inside the Computerجەبر، لۆجیکی بوول و ماتریکسەکان لەناو کۆمپیوتەردا
- Removing Items, Joining and Comparing Listsلابردنی دانەکان، پێکەوەلکاندن و بەراوردکردنی لیستەکان
References and further readingسەرچاوەکان و خوێندنەوەی زیاتر
This lecture is a first edition. The Kurdish text and terminology are waiting for review by Kurdish IT teachers.ئەم وانەیە چاپی یەکەمە. دەقە کوردییەکە و زاراوەکانی چاوەڕێی پێداچوونەوەی مامۆستایانی کوردی بواری IT ن.