MATLABMATLAB · Chapterبەش 4 · Lectureوانە 4.2
Creating Matricesدروستکردنی ماتریکسەکان
Type matrices row by row with spaces and semicolons, use expressions and ranges as elements and rows, join matrices side by side or one under another, build ready-made matrices with zeros, ones, eye, rand and randi, and transpose them with ' (and .' for complex numbers).
ڕیز بە ڕیز بە و بنووسە، و زنجیرە وەک و ڕیز بەکار بهێنە، لە تەنیشت یەک یان لە ژێر یەک پێکەوە بلکێنە، ئامادەکراو بە zeros، ones، eye، rand و randi دروست بکە و بە ' بکە (بە .' بۆ ).
What you will learnچی فێر دەبیت
- Type a matrix row by row and explain why every row needs the same number of elements.
- Use expressions, variables and ranges inside a matrix.
- Join matrices with [A B] and [A; B] and predict when a join fails.
- Build matrices with zeros, ones, eye, rand and randi.
- Transpose a matrix with ' and choose .' for complex data.
- ڕیز بە ڕیز بنووسیت و ڕوونی بکەیتەوە بۆچی هەموو ڕیزێک پێویستی بە هەمان ژمارە هەیە.
- ، و زنجیرە لەناو بەکار بهێنیت.
- بە [A B] و [A; B] پێکەوە بلکێنیت و پێشبینی بکەیت کەی سەرناکەوێت.
- بە zeros، ones، eye، rand و randi دروست بکەیت.
- بە ' بکەیت و بۆ ئاوێتە .' هەڵبژێریت.
A matrix: a table of numbersماتریکس: خشتەیەکی ژمارە
A is a table of numbers arranged in rows (across) and columns (down). Its size is written rows × columns, rows first: a matrix with 3 rows and 4 columns is a matrix. Matrices hold table-like data — the marks of a class, a shop's sales for each day, the pixels of a photo — and they are the main tool of , which science and engineering use everywhere. In MATLAB a matrix is simply a two-dimensional .
خشتەیەکی ژمارەیە کە لە ڕیز (بە ئاسۆیی) و ستوون (لە سەرەوە بۆ خوارەوە) ڕێکخراوە. قەبارەکەی بەم شێوەیە دەنووسرێت: ڕیز × ستوون، ڕیز یەکەم: بە 3 ڕیز و 4 ستوون ـە. خشتەیی هەڵدەگرن — نمرەکانی پۆلێک، فرۆشی ڕۆژانەی دووکانێک، وێنەیەک — و ئامرازی سەرەکیی کە زانست و ئەندازیاری لە هەموو شوێنێک بەکاری دەهێنن. لە MATLABـدا تەنها دووڕەهەندە.
Left: a (: as many rows as columns). Right: a matrix — the number of rows and the number of columns can differ.
چەپ: (: ژمارەی ڕیز و ستوون یەکسانە). ڕاست: — ژمارەی ڕیز و ژمارەی ستوون دەتوانن جیاواز بن.
A matrix: a table of numbersماتریکس: خشتەیەکی ژمارە
Like an egg trayوەک کارتۆنی هێلکە
An egg tray at the bazaar has rows and columns of places, and every row has the same number of places. You name a tray by its shape — “5 by 6” — and you find an egg by its row and its column. A matrix is that tray filled with numbers.
کارتۆنێکی هێلکە لە بازاڕ ڕیز و ستوونی شوێنی هەیە و هەموو ڕیزێک هەمان ژمارە شوێنی هەیە. کارتۆنەکە بە شێوەکەی ناو دەبەیت — «5 لە 6» — و هێلکەیەک بە ڕیز و ستوونەکەی دەدۆزیتەوە. ئەو کارتۆنەیە کە بە ژمارە پڕ کراوە.
Where the analogy stops workingسنووری ئەم نموونەیە
A tray can have an empty place. A matrix cannot: every place holds a number (MATLAB uses NaN when a value is missing).
کارتۆن دەتوانێت شوێنی بەتاڵی هەبێت. ناتوانێت: هەموو شوێنێک ژمارەیەک هەڵدەگرێت (MATLAB بۆ ونبوو NaN بەکار دەهێنێت).
The maths of matrices — the names of the entries, the special matrices, the operations — is taught in the maths lessons: linear algebra and matrices, types of matrices and matrix notation and special matrices. This lesson shows how to type and build matrices in MATLAB.
ماتماتیکی — ناوی ژمارەکانی ناوی، ، ئەو کارانەی لەسەریان دەکرێن — لە وانەکانی ماتماتیکدا دەگوترێتەوە: جەبری هێڵی و ماتریکسەکان، جۆرەکانی ماتریکس و نیشانەکردن و ماتریکسە تایبەتەکان. ئەم وانەیە پیشان دەدات چۆن لە MATLABـدا بنووسیت و دروست بکەیت.
Typing a matrixنووسینی ماتریکسێک
Type a matrix the way you read it, row by row, inside square brackets [ ]. Separate the elements of a row with spaces or commas. End each row with a semicolon ; — or press Enter — to start the next row. This is the same idea as row and column vectors (vectors), now with several rows.
بەو شێوەیە بنووسە کە دەیخوێنیتەوە، ڕیز بە ڕیز، لەناو کەوانەی چوارگۆشە [ ]ـدا. ڕیزێک بە یان کۆما لێک جیا بکەرەوە. کۆتایی هەر ڕیزێک بە ; بهێنە — یان Enter دابگرە — بۆ دەستپێکردنی ڕیزی دواتر. ئەمە هەمان بیرۆکەی ڕیز و ستوونە (ئاڕاستەبڕەکان)، ئێستا بە چەند ڕیزێک.
; starts a new rowMATLAB چۆن نووسینەکەت دەخوێنێتەوە: دەچێتە ڕاست، ; ڕیزێکی نوێ دەست پێدەکاتCêITsales = [42 18 25 30; 37 22 19 28; 51 26 33 35]
r = [6 0 13 2 9
4 11 7 15 1]sales =
42 18 25 30
37 22 19 28
51 26 33 35
r =
6 0 13 2 9
4 11 7 15 1Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 1
Karwan's tea house in Sulaymaniyah: glasses of tea, coffee, juice and water sold on three days. Three rows (days), four columns (drinks): a matrix.
چایخانەی کاروان لە سلێمانی: پەرداخی چا، قاوە، شەربەت و ئاو کە لە سێ ڕۆژدا فرۆشراون. سێ ڕیز (ڕۆژەکان)، چوار ستوون (خواردنەوەکان): .
- Lineدێڕی 2-3
Pressing Enter instead of
;also starts a new row. MATLAB waits for the closing]; the extra spaces at the start of line 3 only make it easier to read.داگرتنی Enter لە جیاتی
;ـیش ڕیزێکی نوێ دەست پێدەکات. MATLAB چاوەڕێی]ـی داخستن دەکات؛ زیادەی سەرەتای دێڕی 3 تەنها خوێندنەوەکەی ئاسانتر دەکەن.
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 2:40-6:58: [ ] starts a matrix, spaces or commas separate columns, ; or Enter starts a new row; the Workspace and Variable Editor show each size.
لە 2:40 تا 6:58 سەیر بکە: [ ] دەست پێدەکات، یان کۆما ستوونەکان جیا دەکەنەوە، ; یان Enter ڕیزێکی نوێ دەست پێدەکات؛ و Variable Editor قەبارەی هەر یەکێکیان پیشان دەدەن.
Typing a matrixنووسینی ماتریکسێک
Every row must have the same lengthهەموو ڕیزێک دەبێت هەمان درێژیی هەبێت
A matrix is a rectangle, so every row must have the same number of elements. If one row is shorter, MATLAB does not guess the missing number: it stops with a red saying that the dimensions of the arrays being joined are not consistent. (Typing ; really stacks one row under another — joining is explained below.)
لاکێشەیە، بۆیە هەموو ڕیزێک دەبێت هەمان ژمارە هەبێت. ئەگەر ڕیزێک کورتتر بێت، MATLAB ژمارە ونبووەکە پێشبینی ناکات: بە سوور دەوەستێت کە دەڵێت لە کاتی ڕەهەندەکان یەک ناگرنەوە. (نووسینی ; لە ڕاستیدا ڕیزێک دەخاتە ژێر ڕیزێکی ترەوە — لە خوارەوە ڕوون دەکرێتەوە.)
bad = [3 8 1; 6 2]
good = [3 8 1; 6 2 NaN]good =
3 8 1
6 2 NaNExplain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 1
Row 1 has three elements, row 2 only two: MATLAB stops with an error and
badis not created.ڕیزی 1 سێ هەیە، ڕیزی 2 تەنها دوو: MATLAB بە دەوەستێت و
badدروست ناکرێت.- Lineدێڕی 2
Fill every place. If a measurement is really missing,
NaN(“not a number”, see special values) keeps the shape.هەموو شوێنێک پڕ بکەرەوە. ئەگەر پێوانەیەک بەڕاستی ونبووە،
NaN(«ژمارە نییە»، بڕوانە نرخەکان تایبەتەکان) شێوەکە دەپارێزێت.
Elements can be expressionsتوخمەکان دەتوانن دەربڕین بن
As in a vector (elements can be expressions), each element of a matrix can be any : a number, a , a calculation or a . MATLAB works out every element first, then builds the matrix.
وەک (توخمەکان دەتوانن دەربڕین بن)، هەر دەتوانێت هەر بێت: ژمارەیەک، ، هەژمارێک یان . MATLAB سەرەتا هەموو هەژمار دەکات، پاشان دروست دەکات.
h = 2; k = 8; r = 5;
E = [h, r*k, sin(pi/6); r^2, sqrt(r*k/h), 11]E =
2.0000 40.0000 0.5000
25.0000 4.4721 11.0000Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 1
Three variables on one line; the semicolons hide their echo.
سێ لە یەک دێڕدا؛ پیشاندانیان دەشارنەوە.
- Lineدێڕی 2
A matrix:
r*k= 40,sin(pi/6)= 0.5,r^2= 25,sqrt(40/2)= 4.4721. Because one element has decimals, MATLAB shows all of them with four decimals.:
r*k= 40،sin(pi/6)= 0.5،r^2= 25،sqrt(40/2)= 4.4721. چونکە یەک ژمارەی دوای هەیە، MATLAB هەموویان بە چوار ژمارەی دوای پیشان دەدات.
Elements can be expressionsتوخمەکان دەتوانن دەربڕین بن
Whole ranges as rowsزنجیرەی تەواو وەک ڕیز
A row can also be a whole range or a linspace vector (ranges, linspace). This is a fast way to build a table — as long as every range gives the same number of elements.
ڕیزێک دەتوانێت زنجیرەیەکی تەواو یان linspace بێت (زنجیرەکان، linspace). ئەمە ڕێگەیەکی خێرایە بۆ دروستکردنی خشتەیەک — بە مەرجێک هەموو زنجیرەیەک هەمان ژمارە بدات.
T = [2:6; 20:-2.5:10; linspace(0, 1, 5)]T =
2.0000 3.0000 4.0000 5.0000 6.0000
20.0000 17.5000 15.0000 12.5000 10.0000
0 0.2500 0.5000 0.7500 1.0000Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 1
Three rows of five: 2 to 6 in steps of 1; 20 down to 10 in steps of ; five points from 0 to 1. An exact 0 is shown without decimals.
سێ ڕیزی پێنجی: 2 تا 6 بە هەنگاوی 1؛ 20 بەرەو خوار تا 10 بە هەنگاوی ؛ پێنج خاڵ لە 0 تا 1. سفری ڕێک بێ ژمارەی دوای پیشان دەدرێت.
Joining matrices: [A B] and [A; B]پێکەوەلکاندنی ماتریکسەکان: [A B] و [A; B]
Square brackets can also join whole matrices; this is called . The same two separators decide the direction:
کەوانەی چوارگۆشە دەتوانێت تەواویش پێکەوە بلکێنێت؛ ئەمە پێی دەگوترێت . هەمان دوو جیاکەرەوە ئاڕاستەکە دیاری دەکەن:
[A B]or[A, B]puts B to the right of A. Both need the same number of rows.[A; B]puts B under A. Both need the same number of columns.- If the sizes do not fit, MATLAB stops with an about inconsistent dimensions — the result would not be a rectangle.
[A B]یان[A, B]B دەخاتە لای ڕاستی A. هەردووکیان پێویستیان بە هەمان ژمارەی ڕیز هەیە.[A; B]B دەخاتە ژێر A. هەردووکیان پێویستیان بە هەمان ژمارەی ستوون هەیە.- ئەگەر قەبارەکان نەگونجێن، MATLAB بە دەربارەی ڕەهەندی ناڕێک دەوەستێت — ئەنجامەکە لاکێشە نابێت.
A = [1 2; 3 4];
B = [7 8 9; 5 6 0];
C = [0 10];
H = [A B]
V = [A; C]
W = [A C]H =
1 2 7 8 9
3 4 5 6 0
V =
1 2
3 4
0 10Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 4
A () and B () both have 2 rows, so they fit side by side: a matrix.
A () و B () هەردووکیان 2 ڕیزیان هەیە، بۆیە لە تەنیشت یەک جێ دەبنەوە: .
- Lineدێڕی 5
A and C both have 2 columns, so C fits underneath: a matrix.
A و C هەردووکیان 2 ستوونیان هەیە، بۆیە C لە ژێرەوە جێ دەبێتەوە: .
- Lineدێڕی 6
A has 2 rows, C only 1: they cannot stand side by side. MATLAB stops with an error and
Wis not created.[A; B]fails too (2 columns against 3).A دوو ڕیزی هەیە، C تەنها یەک: ناتوانن لە تەنیشت یەک بوەستن. MATLAB بە دەوەستێت و
Wدروست ناکرێت.[A; B]ـیش سەرناکەوێت (2 ستوون بەرامبەر 3).
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 19:11-21:56: [row1; row2] stacks rows, [col1 col2] puts columns side by side: concatenation with the same square brackets.
لە 19:11 تا 21:56 سەیر بکە: [row1; row2] ڕیزەکان لە ژێر یەک دادەنێت، [col1 col2] ستوونەکان لە تەنیشت یەک دادەنێت: بە هەمان کەوانە چوارگۆشەکان.
Ready-made matrices: zeros, ones, eyeماتریکسە ئامادەکراوەکان: zeros، ones، eye
Typing every element is slow for a big matrix. MATLAB has that build common matrices from their size alone:
نووسینی هەموو بۆ گەورە هێواشە. MATLAB چەند هەیە کە تەنها لە قەبارەکەیانەوە دروست دەکەن:
| Command | What you getچی وەردەگریت |
|---|---|
zeros(m, n) | an matrix of 0s (a ) پڕ لە 0 () |
ones(m, n) | an matrix of 1s پڕ لە 1 |
eye(n) | the : 1s on the , 0s elsewhere (the name sounds like the letter I) بە قەبارەی : 1 لەسەر و 0 لە شوێنەکانی تر (ناوەکە وەک پیتی I دەخوێنرێتەوە) |
rand(m, n) | an matrix of between 0 and 1 (next screen) پڕ لە لە نێوان 0 و 1 (شاشەی دواتر) |
zeros(n) | one number means a square matrix — zeros(3) is , not یەک ژمارە واتە — zeros(3) ـە، نەک |
z = zeros(2,4)
w = ones(3,2)
I = eye(3)
zeros(3)z =
0 0 0 0
0 0 0 0
w =
1 1
1 1
1 1
I =
1 0 0
0 1 0
0 0 1
ans =
0 0 0
0 0 0
0 0 0Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 1-2
Rows first, then columns, as always:
zeros(2,4)has 2 rows and 4 columns.zeros(1,3)andones(1,3)make row vectors.وەک هەمیشە ڕیز یەکەمە، پاشان ستوون:
zeros(2,4)2 ڕیز و 4 ستوونی هەیە.zeros(1,3)وones(1,3)ڕیز دروست دەکەن.- Lineدێڕی 3
The plays the part of 1 in matrix multiplication; its maths is in special matrices.
لە لێکدانی ڕۆڵی 1 دەبینێت؛ ماتماتیکەکەی لە ماتریکسە تایبەتەکاندایە.
- Lineدێڕی 4
One gives a square matrix.
یەک دەدات.
A matrix of zeros is a good starting table: make it with the right size, then fill its places one by one (changing elements).
سفر خشتەیەکی دەستپێکی باشە: بە قەبارە دروستەکەی دروستی بکە، پاشان شوێنەکانی یەک بە یەک پڕ بکەرەوە (گۆڕینی توخمەکان).
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 21:57-25:01: zeros(4,6), ones, eye(5), diag([1 2 3 4 5]) and rand(3,4) with values between 0 and 1.
لە 21:57 تا 25:01 سەیر بکە: zeros(4,6)، ones، eye(5)، diag([1 2 3 4 5]) و rand(3,4) بە نێوان 0 و 1.
Ready-made matrices: zeros, ones, eyeماتریکسە ئامادەکراوەکان: zeros، ones، eye
Matrices of random numbersماتریکسی ژمارە هەڕەمەکییەکان
rand(m, n) fills an matrix with drawn evenly from the interval : every value between 0 and 1 is equally likely, and 0 and 1 themselves never appear. Each call gives new numbers, which is useful for simulations, games and test data. For whole numbers use randi(imax, m, n): random integers from 1 to imax.
rand(m, n) بە پڕ دەکاتەوە کە بە یەکسانی لە هەڵدەبژێردرێن: هەموو لە نێوان 0 و 1 هەمان دەرفەتی هەیە و خودی 0 و 1 هەرگیز دەرناکەون. هەر بانگکردنێک ژمارەی نوێ دەدات، ئەمەش بۆ لاساییکردنەوە، یاری و تاقیکردنەوە بەسوودە. بۆ randi(imax, m, n) بەکار بهێنە: لە 1 تا imax، بێ .
R = rand(2,3)
dice = randi(6, 1, 8)R =
0.6251 0.8972 0.7757
0.2252 0.3002 0.8736
dice =
6 1 3 5 1 5 1 3Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 1
A matrix of random numbers between 0 and 1.
rand(1,3)gives a row of three.لە لە نێوان 0 و 1.
rand(1,3)ڕیزێکی سێیی دەدات.- Lineدێڕی 2
Eight throws of a dice: whole numbers from 1 to 6.
هەشت هەڵدانی زار: لە 1 تا 6.
The transpose: 'جێگۆڕکێی ماتریکس: '
A single quote ' after a matrix gives its : row 1 becomes column 1, row 2 becomes column 2, and so on. An matrix becomes . You met ' on vectors (row to column); on a matrix it turns every row into a column. The maths is in the transpose — a transpose is a mirror across the , not a turn (a mirror, not a turn) — and its rules are in the transpose of sums and products.
کۆتەیشنێکی تاک ' لە دوای دەدات: ڕیزی 1 دەبێتە ستوونی 1، ڕیزی 2 دەبێتە ستوونی 2 و هتد. دەبێتە . پێشتر 'ـت لەسەر بینی (لە ڕیزەوە بۆ ستوون)؛ لەسەر هەموو ڕیزێک دەکات بە ستوون. ماتماتیکەکەی لە جێگۆڕکێی ماتریکسدایە — ئاوێنەیە بە درێژایی ، نەک سووڕانەوە (ئاوێنە، نەک سووڕانەوە) — و یاساکانی لە جێگۆڕکێی ماتریکس لە کۆ و لێکداندادان.
![Animation: the 2 × 3 matrix A = [4 7 2; 9 1 6]. Row 1 (4 7 2) is highlighted, lifts and settles as column 1 of a new matrix; then row 2 (9 1 6) settles as column 2. The last frame shows A' = [4 9; 7 1; 2 6] with the sizes 2 × 3 and 3 × 2 labelled.](/_astro/matlab-matrices-transpose.en.CB7pO8Qh.gif)
![Animation: the 2 × 3 matrix A = [4 7 2; 9 1 6]. Row 1 (4 7 2) is highlighted, lifts and settles as column 1 of a new matrix; then row 2 (9 1 6) settles as column 2. The last frame shows A' = [4 9; 7 1; 2 6] with the sizes 2 × 3 and 3 × 2 labelled.](/_astro/matlab-matrices-transpose.ckb.mwtQ3gfY.gif)
C = [3 10 7 1; 12 5 0 8; 4 9 6 2];
D = C'
col = [4 9 2]'D =
3 12 4
10 5 9
7 0 6
1 8 2
col =
4
9
2Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 2
Row 1 of C (3 10 7 1) is now column 1 of D, and so on.
ڕیزی 1ی C (3 10 7 1) ئێستا ستوونی 1ی Dـیە و هتد.
- Lineدێڕی 3
On a row vector the transpose gives a column vector.
لەسەر ڕیز، ستوون دەدات.
The transpose: 'جێگۆڕکێی ماتریکس: '
' or .'? Complex numbers' یان .'؟ ژمارە ئاوێتەکان
For real numbers ' is the whole story. For it does one more thing: it also takes the of every element (, see complex numbers). Its full name is the complex conjugate transpose. The plain transpose, which only swaps rows and columns, is written .' (dot, quote).
بۆ ' هەموو شتەکەیە. بۆ کارێکی تریش دەکات: هەموو وەردەگرێت (، بڕوانە ژمارە ئاوێتەکان). ناوە تەواوەکەی بە ئینگلیزی ئەمەیە: complex conjugate transpose. ساکار، کە تەنها ڕیز و ستوون دەگۆڕێتەوە، بە .' (خاڵ، کۆتەیشن) دەنووسرێت.
z = [1+2i, 3-1i];
z'
z.'ans = 1.0000 - 2.0000i 3.0000 + 1.0000i ans = 1.0000 + 2.0000i 3.0000 - 1.0000i
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 2
': a column, and every imaginary part changed its sign.': ستوونێک و هەموو بەشێکی خەیاڵی نیشانەکەی گۆڕا.- Lineدێڕی 3
.': the same column with the numbers unchanged..': هەمان ستوون و ژمارەکان نەگۆڕاون.
Vectors and numbers are matrices tooئاڕاستەبڕەکان و ژمارەکانیش ماتریکسن
For MATLAB a row vector is a matrix, a column vector is an matrix and a single number is a matrix (every value is an array). That is why the same brackets, ;, ' and joining rules work for all of them. To ask MATLAB for the size of any of them, use size (measuring arrays).
بۆ MATLAB ڕیز ـە، ستوون ـە و تەنها یەک ژمارە ـە (هەموو نرخێک ڕیزەکێکە). لەبەر ئەوە هەمان کەوانە، ;، ' و یاساکانی بۆ هەموویان کار دەکەن. بۆ پرسیارکردن لە قەبارەی هەر یەکێکیان، size بەکار بهێنە (پێوانەکردنی ڕیزەکەکان).
v = [5 8 1];
x = 7;
both = [v x]
stack = [v; 2 4 6]both =
5 8 1 7
stack =
5 8 1
2 4 6Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 3
A vector and a number both have one row, so they join side by side.
و ژمارەیەکی هەردووکیان یەک ڕیزیان هەیە، بۆیە لە تەنیشت یەک دەلکێن.
- Lineدێڕی 4
A vector and a new row of the same length stack into a matrix.
و ڕیزێکی نوێی هەمان درێژی دەبنە .
If you know Python: there a table is a list of lists, [[1, 2], [3, 4]] (a list of rows). Typed in MATLAB, [[1,2],[3,4]] only joins two rows side by side and gives the row 1 2 3 4 — write [1 2; 3 4] instead.
ئەگەر Python دەزانیت: لەوێ خشتە لیستێکە لە لیست، [[1, 2], [3, 4]] (لیستێک لە ڕیز). ئەگەر لە MATLABـدا بنووسرێت، [[1,2],[3,4]] تەنها دوو ڕیز لە تەنیشت یەک دەلکێنێت و ڕیزی 1 2 3 4 دەدات — لە جیاتی ئەوە بنووسە [1 2; 3 4].
Common mistakesهەڵە باوەکان
| Mistakeهەڵە | What happensچی ڕوودەدات | Fixچارەسەر |
|---|---|---|
Rows of different lengths: [1 2 3; 4 5]ڕیزی جیاواز لە درێژیدا: [1 2 3; 4 5] | An error; no matrix is made؛ هیچ دروست ناکرێت | Give every row the same number of elements (use NaN for a missing value).هەمان ژمارە بدە بە هەموو ڕیزێک (بۆ ونبوو NaN بەکار بهێنە). |
Joining blocks whose sizes do not fit: [A C] with 2 and 1 rows پارچەی ناگونجاو: [A C] بە 2 و 1 ڕیز | An error about inconsistent dimensions دەربارەی ڕەهەندی ناڕێک | Side by side: equal rows. Stacked: equal columns.لە تەنیشت یەک: ڕیزی یەکسان. لە ژێر یەک: ستوونی یەکسان. |
Expecting zeros(3) to be چاوەڕوانیی ئەوەی zeros(3) بێت | A matrix of zeros لە سفر | zeros(1,3) for a row of threezeros(1,3) بۆ ڕیزێکی سێیی |
Swapping the sizes: zeros(4,2) for “2 rows, 4 columns”گۆڕینەوەی قەبارەکان: zeros(4,2) بۆ «2 ڕیز، 4 ستوون» | A matrix | Rows first: zeros(2,4).ڕیز یەکەمە: zeros(2,4). |
Using ' on complex data when you only want to swap rows and columnsبەکارهێنانی ' لەسەر ئاوێتە کاتێک تەنها دەتەوێت ڕیز و ستوون بگۆڕیتەوە | Every imaginary part changes its signهەموو بەشێکی خەیاڵی نیشانەکەی دەگۆڕێت | Use .'..' بەکار بهێنە. |
Python-style rows: [[1,2],[3,4]]ڕیزی شێوەی Python: [[1,2],[3,4]] | A single row 1 2 3 4, not a matrixیەک ڕیزی 1 2 3 4، نەک | [1 2; 3 4][1 2; 3 4] |
Capital letters: Zeros(2), Eye(3)پیتی گەورە: Zeros(2)، Eye(3) | An error: MATLAB is case-sensitive and knows only zeros, eye: MATLAB و تەنها zeros و eye دەناسێت | Write function names in lower case.ناوی بە پیتی بچووک بنووسە. |
Practiceڕاهێنان
Aram, Shilan and Dilan each have four marks: Aram 78, 85, 62, 90; Shilan 91, 74, 88, 69; Dilan 55, 80, 73, 95. Store them in a matrix marks with one row per student. What is its size?
ئارام، شیلان و دیلان هەر یەکەیان چوار نمرەیان هەیە: ئارام 78، 85، 62، 90؛ شیلان 91، 74، 88، 69؛ دیلان 55، 80، 73، 95. لە بە ناوی marksـدا هەڵیانبگرە، هەر قوتابییەک یەک ڕیز. قەبارەکەی چەندە؟
Show answerوەڵامەکە پیشان بدە
marks = [78 85 62 90; 91 74 88 69; 55 80 73 95]marks =
78 85 62 90
91 74 88 69
55 80 73 95Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 1
Three students (rows) × four subjects (columns): a matrix.
سێ قوتابی (ڕیز) × چوار وانە (ستوون): .
Build a matrix X in one command: row 1 is 1 to 5, row 2 the even numbers 2 to 10, row 3 counts down from 100 in steps of 20.
بە ناوی X بە یەک دروست بکە: ڕیزی 1 لە 1 تا 5، ڕیزی 2 ژمارە جووتەکانی 2 تا 10، ڕیزی 3 لە 100ـەوە بە هەنگاوی 20 بەرەو خوار دەژمێرێت.
Show answerوەڵامەکە پیشان بدە
X = [1:5; 2:2:10; 100:-20:20]X =
1 2 3 4 5
2 4 6 8 10
100 80 60 40 20Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 1
Each range has five elements, so the rows fit.
هەر زنجیرەیەک پێنج هەیە، بۆیە ڕیزەکان دەگونجێن.
Without typing every element, build J, a matrix whose first two columns are ones and last three columns are zeros, and K, the with the row 7 7 under it.
بێ نووسینی هەموو ، J دروست بکە، کە دوو ستوونی یەکەمی یەک بن و سێ ستوونی کۆتایی سفر، هەروەها K، بە قەبارەی و ڕیزی 7 7 لە ژێریدا.
Show answerوەڵامەکە پیشان بدە
J = [ones(2) zeros(2,3)]
K = [eye(2); 7 7]J =
1 1 0 0 0
1 1 0 0 0
K =
1 0
0 1
7 7Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 1
next to : both have 2 rows.
لە تەنیشت : هەردووکیان 2 ڕیزیان هەیە.
- Lineدێڕی 2
over : both have 2 columns.
لە سەر : هەردووکیان 2 ستوونیان هەیە.
Write the size of each result, or say that it gives an error: (a) zeros(2,5)' (b) [eye(3) ones(3,1)] (c) [ones(1,4); zeros(2,4)] (d) [eye(2); ones(2,3)].
قەبارەی هەر ئەنجامێک بنووسە، یان بڵێ دەدات: (أ) zeros(2,5)' (ب) [eye(3) ones(3,1)] (ج) [ones(1,4); zeros(2,4)] (د) [eye(2); ones(2,3)].
Show answerوەڵامەکە پیشان بدە
(a) — the transpose of a . (b) — 3 rows each, columns. (c) — 4 columns each, rows. (d) An error: stacking needs equal columns, and .
(أ) — . (ب) — هەر یەکەیان 3 ڕیز، ستوون. (ج) — هەر یەکەیان 4 ستوون، ڕیز. (د) : خستنە ژێر یەک ستوونی یەکسانی پێویستە و .
Write the transpose of on paper, then check with ' in MATLAB.
لەسەر کاغەز بنووسە، پاشان لە MATLABـدا بە ' بیپشکنە.
Show answerوەڵامەکە پیشان بدە
[6 0 3 8; 1 5 9 2]'ans =
6 1
0 5
3 9
8 2Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 1
becomes : row 1 (6 0 3 8) is now column 1.
دەبێتە : ڕیزی 1 (6 0 3 8) ئێستا ستوونی 1ـە.
Each line stops with an error. Find the mistake and correct it: T = [1 2 3; 4 5], Z = Zeros(2,3), U = [ones(2,2) zeros(3,2)].
هەر دێڕێک بە دەوەستێت. هەڵەکە بدۆزەرەوە و ڕاستی بکەرەوە: T = [1 2 3; 4 5]، Z = Zeros(2,3)، U = [ones(2,2) zeros(3,2)].
Show answerوەڵامەکە پیشان بدە
T: row 2 is short — e.g.T = [1 2 3; 4 5 6].Z: lower case —Z = zeros(2,3).U: 2 rows next to 3 rows — make the rows equal, e.g.U = [ones(2,2) zeros(2,2)], or stack them:[ones(2,2); zeros(3,2)](both have 2 columns).
T: ڕیزی 2 کورتە — بۆ نموونەT = [1 2 3; 4 5 6].Z: پیتی بچووک —Z = zeros(2,3).U: 2 ڕیز لە تەنیشت 3 ڕیز — ڕیزەکان یەکسان بکە، بۆ نموونەU = [ones(2,2) zeros(2,2)]، یان بیانخە ژێر یەک:[ones(2,2); zeros(3,2)](هەردووکیان 2 ستوونیان هەیە).
Summaryپوختە
- A matrix is a table of numbers; its size is rows × columns (rows first).
- Type it row by row in
[ ]: spaces or commas between elements,;or Enter between rows. Every row must have the same length. - Elements may be expressions, variables and function calls; rows may be ranges of equal length.
[A B]joins side by side (equal rows);[A; B]stacks (equal columns).zeros(m,n),ones(m,n),eye(n),rand(m,n),randi(imax,m,n); one argument means a square matrix.'transposes () and also conjugates complex numbers;.'only transposes.
- خشتەیەکی ژمارەیە؛ قەبارەکەی ڕیز × ستوونە (ڕیز یەکەم).
- ڕیز بە ڕیز لەناو
[ ]بینووسە: یان کۆما لە نێوان ،;یان Enter لە نێوان ڕیزەکان. هەموو ڕیزێک دەبێت هەمان درێژیی هەبێت. - دەتوانن ، و بن؛ ڕیزەکان دەتوانن زنجیرەی هاودرێژ بن.
[A B]لە تەنیشت یەک دەلکێنێت (ڕیزی یەکسان)؛[A; B]دەیانخاتە ژێر یەک (ستوونی یەکسان).zeros(m,n)،ones(m,n)،eye(n)،rand(m,n)،randi(imax,m,n)؛ یەک واتە .'دەکات () و بۆ وەردەگرێت؛.'تەنها دەکات.
How do you type a matrix with the rows 1 2 3 and 4 5 6?چۆن ماتریکسێکی بە ڕیزەکانی 1 2 3 و 4 5 6 دەنووسیت؟
[1 2 3; 4 5 6] (or with commas, or with Enter between the rows).
[1 2 3; 4 5 6] (یان بە کۆما، یان بە Enter لە نێوان ڕیزەکاندا).
When does [A; B] work?[A; B] کەی کار دەکات؟
When A and B have the same number of columns.
کاتێک A و B هەمان ژمارەی ستوونیان هەبێت.
What is the difference between ' and .'?جیاوازیی نێوان ' و .' چییە؟
Both swap rows and columns. ' also replaces every complex number by its conjugate; for real numbers they give the same result.
هەردووکیان ڕیز و ستوون دەگۆڕنەوە. ' هەروەها هەموو بە دەگۆڕێت؛ بۆ هەمان ئەنجام دەدەن.
Lesson complete!وانەکە تەواو بوو!
Well done. Review the key terms, then go on to the next lesson.دەستخۆش. سەیرێکی زاراوە سەرەکییەکان بکەرەوە، پاشان بڕۆ بۆ وانەی داهاتوو.
Key termsزاراوە سەرەکییەکان
Indexing, the Colon, Adding and Deleting Elementsئیندێکسەکان، دوو خاڵ، زیادکردن و سڕینەوەی توخمەکان
Related lecturesوانە پەیوەندیدارەکان
- Indexing, the Colon, Adding and Deleting Elementsئیندێکسەکان، دوو خاڵ، زیادکردن و سڕینەوەی توخمەکان
- Functions for Arraysکردارەکانی ڕیزەک
- Matrix Operations vs Element-by-Element Operationsکارەکانی ماتریکس بەرامبەر کارەکانی توخم بە توخم
- Algebra, Boolean Logic and Matrices Inside the Computerجەبر، لۆجیکی بوول و ماتریکسەکان لەناو کۆمپیوتەردا
- Matrix Notation and Special Matricesنووسینی ماتریکس و جۆرە تایبەتەکانی
- Multiplying Matricesلێکدانی ماتریکسەکان
- Two-Dimensional 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 ن.