MATLABMATLAB · Chapterبەش 4 · Lectureوانە 4.3
Indexing, the Colon, Adding and Deleting Elementsئیندێکسەکان، دوو خاڵ، زیادکردن و سڕینەوەی توخمەکان
Reach one element with v(k) or A(r,c) (MATLAB counts from 1, end is the last index), understand index errors and linear indexing down the columns, take rows, columns and blocks with the colon and index vectors, grow arrays by assigning outside them (zero fill) and delete elements, rows and columns with [].
بە v(k) یان A(r,c) بگە بە یەک (MATLAB لە 1ـەوە دەژمێرێت، end دوایین )، و بە درێژایی ستوونەکان تێبگە، بە دوو خاڵ و ڕیز، ستوون و پارچە وەربگرە، بە دانانی لە دەرەوەیان گەورە بکە (پڕکردنەوە بە سفر) و بە [] ، ڕیز و ستوون بسڕەوە.
What you will learnچی فێر دەبیت
- Read and change one element of a vector or matrix, using end for the last index.
- Explain index errors and linear indexing in column order.
- Take whole rows, columns and blocks with the colon and index vectors.
- Add rows and columns and predict where MATLAB fills in zeros.
- Delete elements of a vector and whole rows or columns of a matrix.
- یەک یان بخوێنیتەوە و بیگۆڕیت، بە بەکارهێنانی end بۆ دوایین .
- و بە ڕیزبەندیی ستوون ڕوون بکەیتەوە.
- بە دوو خاڵ و ڕیز، ستوون و پارچەی تەواو وەربگریت.
- ڕیز و ستوون زیاد بکەیت و پێشبینی بکەیت MATLAB لە کوێ سفر دادەنێت.
- و ڕیز یان ستوونی تەواوی بسڕیتەوە.
Reaching one elementگەیشتن بە یەک توخم
A vector keeps many values under one name (why vectors). To reach one of them, write its position — its — in round brackets after the name: jars(3) is the third element. In a matrix you give two indexes, row first, then column: A(2,3) is the element in row 2, column 3 — the same order as in maths (the address of an entry).
چەندین لەژێر یەک ناودا هەڵدەگرێت (بۆچی ئاڕاستەبڕەکان). بۆ گەیشتن بە یەکێکیان، شوێنەکەی — — لەناو کەوانەی بازنەییدا لە دوای ناوەکە بنووسە: jars(3) سێیەم . لە دوو دەدەیت، سەرەتا ڕیز، پاشان ستوون: A(2,3) ئەو کە لە ڕیزی 2 و ستوونی 3دایە — هەمان ڕیزبەندیی لە ماتماتیکدا (ناونیشانی ژمارەیەک).
MATLAB counts from 1, like mathematics: the first element is v(1). Many other languages (Python, C, Java) count from 0. Every example in this lesson also runs in GNU Octave and in the free version of MATLAB Online.
MATLAB لە 1ـەوە دەژمێرێت، وەک ماتماتیک: یەکەم v(1)ـە. زۆر زمانی تر (Python، C، Java) لە 0ـەوە دەژمێرن. هەموو نموونەیەکی ئەم وانەیە لە GNU Octave و لە وەشانی بەخۆڕایی MATLAB Onlineـیشدا کار دەکات.
jars = [250 100 500 200 150 400];
jars(3)
A = [6 1 8 3; 4 9 2 7; 5 0 11 12];
A(2,3)
A(3,2)ans =
500
ans =
2
ans =
0Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 1-2
The spice shelf of the vectors lesson: six jar weights in grams.
jars(3)is the third jar, 500.ڕەفەی بەهاراتی وانەی : کێشی شەش دەفر بە گرام.
jars(3)سێیەم دەفرە، 500.- Lineدێڕی 4-5
Order matters:
A(2,3)(row 2, column 3) is 2, butA(3,2)(row 3, column 2) is 0.ڕیزبەندی گرنگە:
A(2,3)(ڕیزی 2، ستوونی 3) 2ـە، بەڵامA(3,2)(ڕیزی 3، ستوونی 2) 0ـە.
Reaching one elementگەیشتن بە یەک توخم
end: the last indexend: دوایین ئیندێکس
Inside the brackets, the end means the last index of that dimension. You do not need to know the size: v(end) is the last element, v(end-1) the one before it, A(end,1) the first element of the last row and A(2,end) the last element of row 2.
لەناو کەوانەکەدا، end واتە دوایین لەو ڕەهەندەدا. پێویست ناکات قەبارەکە بزانیت: v(end) دوایین ، v(end-1) ئەوەی پێش ئەو، A(end,1) یەکەم دوایین ڕیز و A(2,end) دوایین ڕیزی 2.
jars(end)
jars(end-1)
A(end,1)
A(2,end)
A(end,end)ans =
400
ans =
150
ans =
5
ans =
7
ans =
12Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 3-5
endcan stand for the last row, the last column, or both.endدەتوانێت جێگەی دوایین ڕیز، دوایین ستوون یان هەردووکیان بگرێتەوە.
Reaching one elementگەیشتن بە یەک توخم
Changing one elementگۆڕینی یەک توخم
An indexed element can stand on the left of =: the new value replaces the old one and the rest of the array stays the same.
بە دەتوانێت لە لای چەپی = بوەستێت: نوێ جێگەی کۆنەکە دەگرێتەوە و باقیی وەک خۆی دەمێنێتەوە.
jars(2) = 120
A(3,1) = 10jars =
250 120 500 200 150 400
A =
6 1 8 3
4 9 2 7
10 0 11 12Index errorsهەڵەکانی ئیندێکس
An index must be a positive whole number that points inside the array. MATLAB stops with a red when:
دەبێت موجەب بێت کە ئاماژە بۆ ناو بکات. MATLAB لەم بارانەدا بە سوور دەوەستێت:
- the index is 0, negative or a fraction:
A(0),A(-2),A(1.5)— the message says that array indices must be positive integers (or logical values); - you read past the end:
A(4,2)when A has only 3 rows, orjars(7)whenjarshas 6 elements — the message says the index is outside the array's bounds.
- 0، سالب یان بێت:
A(0)،A(-2)،A(1.5)— دەڵێت دەبێت و موجەب (یان لۆجیکی) بن؛ - لە دوای کۆتایی بخوێنیتەوە:
A(4,2)کاتێک A تەنها 3 ڕیزی هەیە، یانjars(7)کاتێکjars6 هەیە — دەڵێت لە دەرەوەی سنووری .
A(0)
A(-2)
A(4,2)
jars(7)Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 1-2
Not positive: an error. Python's
values[-1](the last element) has no MATLAB twin — writev(end)instead.موجەب نییە: .
values[-1]ـی Python (دوایین ) لە MATLABـدا هاوتای نییە — لە جیاتی ئەوەv(end)بنووسە.- Lineدێڕی 3-4
Outside the array when you read: an error. (Writing outside is different: it makes the array grow — see below.)
لە دەرەوەی کاتێک دەخوێنیتەوە: . (نووسین لە دەرەوە جیاوازە: گەورە دەکات — لە خوارەوە ببینە.)
One index for a matrix: linear indexingیەک ئیندێکس بۆ ماتریکسێک: ئیندێکسی هێڵی
MATLAB stores a matrix in memory column by column: first all of column 1, then column 2, and so on. So you can also reach an element of a matrix with one number, its , counting down the columns. In our matrix, A(6) is the 6th element counted this way: row 3 of column 2.
MATLAB ستوون بە ستوون لە هەڵدەگرێت: سەرەتا هەموو ستوونی 1، پاشان ستوونی 2 و هتد. بۆیە دەتوانیت بە یەک ژمارەش بگەیتە ، بە ، کە بە درێژایی ستوونەکان دەژمێردرێت. لە ـی ئێمەدا، A(6) شەشەم بەم ژماردنە: ڕیزی 3ی ستوونی 2.
A(6) is A(3,2) بە درێژایی ستوونەکان دەڕۆن: A(6) هەمان A(3,2)ـەCêITA = [6 1 8 3; 4 9 2 7; 5 0 11 12];
A(6)
A(8)
A(:)'ans =
0
ans =
2
ans =
6 4 5 1 9 0 8 2 11 3 7 12Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 1
The matrix of the first screen again (we changed
A(3,1)to 10 since then).دیسان شاشەی یەکەم (لەو کاتەوە
A(3,1)ـمان کرد بە 10).- Lineدێڕی 2
Count down column 1 (6, 4, 5), then column 2 (1, 9, 0): the 6th is 0, the same as
A(3,2).بە درێژایی ستوونی 1 بژمێرە (6، 4، 5)، پاشان ستوونی 2 (1، 9، 0): شەشەم 0ـە، هەمان
A(3,2).- Lineدێڕی 3
The 8th: second element of column 3, so
A(8)isA(2,3), which is 2.هەشتەم: دووەم ستوونی 3، بۆیە
A(8)هەمانA(2,3)ـە، واتە 2.- Lineدێڕی 4
A(:)lists every element in storage order as one long column; the'only turns it into a row to save space.A(:)هەموو بە ڕیزبەندیی هەڵگرتن وەک یەک ستوونی درێژ لیست دەکات؛'تەنها دەیکات بە ڕیز بۆ پاشەکەوتی شوێن.
Several elements at once: the colonچەند توخمێک بە یەکجار: دوو خاڵ
Inside the brackets the colon means “from … to …”: v(m:n) gives the elements m through n — a new, shorter vector. A colon on its own, v(:), means all elements (as a column). end works here too: v(end-2:end) is the last three.
لەناو کەوانەکەدا دوو خاڵ واتە «لە … تا …»: v(m:n) m تا n دەدات — نوێی کورتتر. دوو خاڵی تەنها، v(:)، واتە هەموو (وەک ستوونێک). end لێرەشدا کار دەکات: v(end-2:end) سێ دوایینەکەن.
v = [7 3 15 9 21 4 12 18 6];
u = v(3:7)
v(end-2:end)u =
15 9 21 4 12
ans =
12 18 6Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 2
Elements 3, 4, 5, 6 and 7. Unlike a Python slice, the last index (7) is included.
3، 4، 5، 6 و 7. بە پێچەوانەی پارچەکردنی Python، دوایین (7) دەگیرێتەوە.
- Lineدێڕی 3
From the third-from-last to the last element.
لە سێیەم لە کۆتاییەوە تا دوایین .
Several elements at once: the colonچەند توخمێک بە یەکجار: دوو خاڵ
Picking chosen elements: an index vectorهەڵبژاردنی توخمەکان: ئاڕاستەبڕی ئیندێکس
The index does not have to be a range. Any vector of positions works, and ranges can be mixed into it: w([2, 4, 8:11]) picks elements 2, 4, 8, 9, 10 and 11, in that order.
پێویست ناکات زنجیرە بێت. هەر شوێنەکان کار دەکات و دەکرێت زنجیرەشی تێکەڵ بکرێت: w([2, 4, 8:11]) 2، 4، 8، 9، 10 و 11 بەو ڕیزبەندییە هەڵدەبژێرێت.
w = 5:4:45
w([2, 4, 8:11])w =
5 9 13 17 21 25 29 33 37 41 45
ans =
9 17 33 37 41 45Rows, columns and blocksڕیز، ستوون و پارچە
In a matrix, a colon in place of one index means the whole row or column. With ranges in both places you cut out a rectangular block:
لە ، دوو خاڵ لە جیاتی یەکێک لە واتە ڕیز یان ستوونی تەواو. بە زنجیرە لە هەردوو شوێنەکەدا پارچەیەکی لاکێشەیی دەبڕیت:
| Command | Meaningواتا |
|---|---|
A(:, n) | column n, all rowsستوونی n، هەموو ڕیزەکان |
A(n, :) | row n, all columnsڕیزی n، هەموو ستوونەکان |
A(:, m:n) | columns m to nستوونەکانی m تا n |
A(m:n, :) | rows m to nڕیزەکانی m تا n |
A(m:n, p:q) | rows m to n and columns p to q: a blockڕیزەکانی m تا n و ستوونەکانی p تا q: پارچەیەک |
N = [2 4 6 8 10 12; 1 3 5 7 9 11; 10 20 30 40 50 60; 7 14 21 28 35 42; 9 8 7 6 5 4];
c3 = N(:,3)
r2 = N(2,:)
rows = N(2:4,:)
block = N(1:3,2:4)c3 =
6
5
30
21
7
r2 =
1 3 5 7 9 11
rows =
1 3 5 7 9 11
10 20 30 40 50 60
7 14 21 28 35 42
block =
4 6 8
3 5 7
20 30 40Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 2
Column 3 comes out as a column vector.
ستوونی 3 وەک ستوون دەردەچێت.
- Lineدێڕی 5
Rows 1–3 and columns 2–4: a block.
ڕیزەکانی 1–3 و ستوونەکانی 2–4: پارچەیەکی .
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 14:17-18:14: C(1,2), C(1,end), a whole column C(:,2), a whole row C(3,:) and a block C(2:3,1:2).
لە 14:17 تا 18:14 سەیر بکە: C(1,2)، C(1,end)، ستوونێکی تەواو C(:,2)، ڕیزێکی تەواو C(3,:) و پارچەیەک C(2:3,1:2).
Rows, columns and blocksڕیز، ستوون و پارچە
Build a table, then read its partsخشتەیەک دروست بکە، پاشان بەشەکانی بخوێنەوە
Rows made from ranges and ready-made matrices (creating matrices) give a quick table to practise on. Here a matrix is built in one line and then read in parts:
ڕیزی دروستکراو لە زنجیرە و ئامادەکراو (دروستکردنی ماتریکسەکان) خشتەیەکی خێرا بۆ ڕاهێنان دەدەن. لێرەدا بە یەک دێڕ دروست دەکرێت و پاشان بەش بە بەش دەخوێنرێتەوە:
R = [9:-1:3; ones(1,7); 3:3:21; zeros(1,7)]
R(3, 2:4)
R(:, end)
R([1 3], [1 7])R =
9 8 7 6 5 4 3
1 1 1 1 1 1 1
3 6 9 12 15 18 21
0 0 0 0 0 0 0
ans =
6 9 12
ans =
3
1
21
0
ans =
9 3
3 21Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 1
Four rows of seven: 9 down to 3, seven ones, 3 to 21 in steps of 3, seven zeros.
چوار ڕیزی حەوتی: 9 بەرەو خوار تا 3، حەوت یەک، 3 تا 21 بە هەنگاوی 3، حەوت سفر.
- Lineدێڕی 2
Row 3, columns 2 to 4.
ڕیزی 3، ستوونەکانی 2 تا 4.
- Lineدێڕی 3
The last column.
دوایین ستوون.
- Lineدێڕی 4
Index vectors in both places: the corners of rows 1 and 3.
لە هەردوو شوێنەکەدا: گۆشەکانی ڕیزەکانی 1 و 3.
Adding rows and columnsزیادکردنی ڕیز و ستوون
To add a row or a column, assign to a position that does not exist yet. The new part must fit: a new row needs as many elements as there are columns. A single number on the right fills the whole new row.
بۆ زیادکردنی ڕیزێک یان ستوونێک، بدە بە شوێنێک کە هێشتا بوونی نییە. بەشە نوێیەکە دەبێت بگونجێت: ڕیزێکی نوێ پێویستی بە هەمان ژمارە هەیە کە ستوونەکان هەن. تەنها یەک ژمارە لە لای ڕاست هەموو ڕیزە نوێیەکە پڕ دەکاتەوە.
G = [3 1 4 1; 5 9 2 6];
G(3,:) = 10:5:25
G(4,:) = 2G =
3 1 4 1
5 9 2 6
10 15 20 25
G =
3 1 4 1
5 9 2 6
10 15 20 25
2 2 2 2Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 2
Row 3 did not exist: the range 10 15 20 25 (four elements for four columns) becomes the new row.
ڕیزی 3 بوونی نەبوو: زنجیرەی 10 15 20 25 (چوار بۆ چوار ستوون) دەبێتە ڕیزە نوێیەکە.
- Lineدێڕی 3
A single number fills every place of the new row 4.
تەنها یەک ژمارە هەموو شوێنەکانی ڕیزی نوێی 4 پڕ دەکاتەوە.
You can also append an existing variable with square brackets, as long as the sizes fit: H = [G; 9 9 9 9] is G with one more row underneath, and [G K] puts the columns of a matrix K to the right of G when K has as many rows as G (joining matrices).
هەروەها دەتوانیت هەبوو بە کەوانەی چوارگۆشە پێوە بلکێنیت، بە مەرجێک قەبارەکان بگونجێن: H = [G; 9 9 9 9] هەمان Gـیە لەگەڵ ڕیزێکی زیاتر لە خوارەوە و [G K] ستوونەکانی بە ناوی K دەخاتە لای ڕاستی G، کاتێک K هەمان ژمارەی ڕیزی Gـی هەبێت (پێکەوەلکاندنی ماتریکسەکان).
Adding rows and columnsزیادکردنی ڕیز و ستوون
Assigning far outside: MATLAB fills the gap with zerosدانانی نرخ لە دوورەوە: MATLAB کەلێنەکە بە سفر پڕ دەکاتەوە
If you assign to a place beyond the current size, MATLAB makes the array big enough and fills every new place you did not mention with 0. It works the same way for a variable that does not exist yet: MATLAB creates it full of zeros, with your value in its place.
ئەگەر بدەیت بە شوێنێک لە دەرەوەی قەبارەی ئێستا، MATLAB بەقەد پێویست گەورە دەکات و هەموو شوێنێکی نوێ کە باست نەکردووە بە 0 پڕ دەکاتەوە. بۆ کە هێشتا بوونی نییە بە هەمان شێوە کار دەکات: MATLAB پڕ لە سفر دروستی دەکات و لە شوێنی خۆیدا دادەنێت.
![Animation in eight frames. AW = [3 6 9; 8 5 11] is a 2 × 3 grid. After AW(4,5) = 17 the grid grows to 4 × 5: the new cells appear with 0 and 17 lands in row 4, column 5. Then AW(:,2:4) = [] marks columns 2 to 4, they fade out, and the remaining columns 1 and 5 slide together into a 4 × 2 matrix [3 0; 8 0; 0 0; 0 17].](/_astro/matlab-indexing-grow-delete.en.CFDOee6D.gif)
![Animation in eight frames. AW = [3 6 9; 8 5 11] is a 2 × 3 grid. After AW(4,5) = 17 the grid grows to 4 × 5: the new cells appear with 0 and 17 lands in row 4, column 5. Then AW(:,2:4) = [] marks columns 2 to 4, they fade out, and the remaining columns 1 and 5 slide together into a 4 × 2 matrix [3 0; 8 0; 0 0; 0 17].](/_astro/matlab-indexing-grow-delete.ckb.dF27_bNd.gif)
AW(4,5) = 17, then delete columns with AW(:,2:4) = []گەورەکردن بە AW(4,5) = 17، پاشان سڕینەوەی ستوون بە AW(:,2:4) = []CêITAW = [3 6 9; 8 5 11];
AW(4,5) = 17
Q(2,5) = 9AW =
3 6 9 0 0
8 5 11 0 0
0 0 0 0 0
0 0 0 0 17
Q =
0 0 0 0 0
0 0 0 0 9Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 2
AW was ; position (4,5) needs 4 rows and 5 columns. Everything new is 0 except the 17.
AW بوو؛ شوێنی (4,5) پێویستی بە 4 ڕیز و 5 ستوونە. هەموو شتێکی نوێ 0ـە جگە لە 17.
- Lineدێڕی 3
Qdid not exist (if it does, runclear Qfirst): MATLAB makes a matrix of zeros with 9 at (2,5).Qبوونی نەبوو (ئەگەر هەیە، سەرەتاclear Qلێبدە): MATLAB لە سفر دروست دەکات و 9 لە (2,5)دا دادەنێت.
A partial row works the same way: G(6,2:end) = 7 on the matrix G adds row 5 full of zeros (nobody gave it values) and row 6 = 0 7 7 7 — the column you skipped is 0 too.
ڕیزێکی ناتەواویش بە هەمان شێوە کار دەکات: G(6,2:end) = 7 لەسەر Gـی ڕیزی 5 پڕ لە سفر زیاد دەکات (کەس پێ نەداوە) و ڕیزی 6 = 0 7 7 7 — ئەو ستوونەی بازت داوە ئەویش 0ـە.
G(6,2:end) = 7G =
3 1 4 1
5 9 2 6
10 15 20 25
2 2 2 2
0 0 0 0
0 7 7 7Adding rows and columnsزیادکردنی ڕیز و ستوون
Vectors grow the same wayئاڕاستەبڕەکانیش بە هەمان شێوە گەورە دەبن
A vector grows like a matrix. Writing one place past the end adds one element; writing further adds zeros in between. This is handy, but a typing mistake in an index can quietly create a huge array — check the size when results look strange (measuring arrays).
وەک گەورە دەبێت. نووسین لە یەک شوێن دوای کۆتایی یەک زیاد دەکات؛ نووسین دوورتر سفر لە نێوانیاندا زیاد دەکات. ئەمە بەسوودە، بەڵام هەڵەیەکی نووسین لە دەتوانێت بە بێدەنگی زۆر گەورە دروست بکات — کاتێک ئەنجامەکان سەیر دەردەکەون قەبارەکە بپشکنە (پێوانەکردنی ڕیزەکەکان).
w = 1:3;
w(6) = 9w =
1 2 3 0 0 9Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 2
Places 4 and 5 were never given values, so they are 0.
شوێنەکانی 4 و 5 هەرگیز پێ نەدرا، بۆیە 0ن.
Deleting with []سڕینەوە بە []
Assigning the empty matrix [] deletes: d(6) = [] removes element 6 and the vector gets shorter; d(3:6) = [] removes four at once. A matrix can only lose whole rows or whole columns — otherwise it would stop being a rectangle — so S(:,2:4) = [] deletes columns 2 to 4 and S(2,:) = [] deletes row 2.
دانانی بەتاڵ [] دەسڕێتەوە: d(6) = [] 6 لادەبات و کورتتر دەبێتەوە؛ d(3:6) = [] چوار بە یەکجار لادەبات. تەنها دەتوانێت ڕیزی تەواو یان ستوونی تەواو لەدەست بدات — ئەگەرنا لاکێشە نامێنێت — بۆیە S(:,2:4) = [] ستوونەکانی 2 تا 4 دەسڕێتەوە و S(2,:) = [] ڕیزی 2 دەسڕێتەوە.
d = [12 7 30 44 5 61 18 9 27 50];
d(6) = []
d(3:6) = []
S = [8 15 2 31 6; 4 19 27 0 13; 22 3 10 17 9];
S(:,2:4) = []d =
12 7 30 44 5 18 9 27 50
d =
12 7 9 27 50
S =
8 6
4 13
22 9Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 2
61 is gone: 9 elements are left.
61 نەما: 9 ماونەتەوە.
- Lineدێڕی 3
Positions 3 to 6 of the new d (30, 44, 5, 18) are removed.
شوێنەکانی 3 تا 6ی d نوێیەکە (30، 44، 5، 18) لادەبرێن.
- Lineدێڕی 5
Columns 2, 3 and 4 disappear; columns 1 and 5 close up into a matrix.
ستوونەکانی 2، 3 و 4 نامێنن؛ ستوونەکانی 1 و 5 دێنە لای یەک و دروست دەکەن.
Practice on one matrixڕاهێنان لەسەر یەک ماتریکس
Type this matrix and try each step before you open the answer:
ئەم ـە بنووسە و پێش کردنەوەی وەڵامەکە هەر هەنگاوێک تاقی بکەرەوە:
P = [3 8 1 6 10; 7 2 9 4 5; 12 11 0 15 14; 6 13 16 2 8]Show (a) the element in row 3, column 4; (b) the whole second column; (c) columns 2 and 3.
پیشان بدە (أ) ئەو لە ڕیزی 3، ستوونی 4دایە؛ (ب) هەموو ستوونی دووەم؛ (ج) ستوونەکانی 2 و 3.
Show answerوەڵامەکە پیشان بدە
P(3,4)
P(:,2)
P(:,2:3)ans =
15
ans =
8
2
11
13
ans =
8 1
2 9
11 0
13 16(d) Show the block of rows 2–3 and columns 3–5. (e) Delete row 4. (f) Then delete column 5. What size is P at the end?
(د) پارچەی ـی ڕیزەکانی 2–3 و ستوونەکانی 3–5 پیشان بدە. (هـ) ڕیزی 4 بسڕەوە. (و) پاشان ستوونی 5 بسڕەوە. لە کۆتاییدا P چ قەبارەیەکی هەیە؟
Show answerوەڵامەکە پیشان بدە
P(2:3,3:5)
P(4,:) = []
P(:,5) = []ans =
9 4 5
0 15 14
P =
3 8 1 6 10
7 2 9 4 5
12 11 0 15 14
P =
3 8 1 6
7 2 9 4
12 11 0 15Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 3
.
.
Common mistakesهەڵە باوەکان
| Mistakeهەڵە | What happensچی ڕوودەدات | Fixچارەسەر |
|---|---|---|
Counting from 0: v(0) for the first elementژماردن لە 0ـەوە: v(0) بۆ یەکەم | An error: indexes start at 1: لە 1ـەوە دەست پێدەکەن | v(1); and v(end) for the last one.v(1)؛ هەروەها v(end) بۆ دوایینیان. |
Swapping row and column: A(3,2) for “column 3, row 2”گۆڕینەوەی ڕیز و ستوون: A(3,2) بۆ «ستوونی 3، ڕیزی 2» | A different element, or an error if row 3 does not exist تر، یان ئەگەر ڕیزی 3 بوونی نەبێت | Row first, always: A(2,3).هەمیشە ڕیز یەکەمە: A(2,3). |
Expecting A(2) to be the 2nd element of row 1چاوەڕوانیی ئەوەی A(2) دووەم ڕیزی 1 بێت | It is the 2nd element of column 1 (linear indexing)دووەم ستوونی 1ـە () | Use two indexes: A(1,2).دوو بەکار بهێنە: A(1,2). |
Deleting one element of a matrix: A(2,3) = []سڕینەوەی یەک : A(2,3) = [] | An error | Delete a whole row or column, or overwrite the value.ڕیزێک یان ستوونێکی تەواو بسڕەوە، یان بگۆڕە. |
A typing mistake in an index you assign: t(100) = 5 instead of t(10) = 5هەڵەی نووسین لە کە پێ دەدەیت: t(100) = 5 لە جیاتی t(10) = 5 | No error — the array silently grows and fills with zerosهیچ نییە — بە بێدەنگی گەورە دەبێت و بە سفر پڕ دەبێتەوە | Check size after building an array.دوای دروستکردنی size بپشکنە. |
Square brackets for an index: v[3]کەوانەی چوارگۆشە بۆ : v[3] | An error: MATLAB indexes with round brackets: MATLAB بە کەوانەی بازنەیی دەکات | v(3)v(3) |
x^2 on a vector to square every elementx^2 لەسەر بۆ دووجاکردنی هەموو | An error: ^ is the matrix power: ^ | x.^2 (why the dot matters)x.^2 (بۆچی خاڵەکە گرنگە) |
If you know Python: there values[0] is the first element and a slice values[1:4] stops before index 4 (indexes, slices). In MATLAB v(1) is the first element and v(2:4) includes element 4. Choosing elements by a condition, such as v(v > 5), needs comparison operators and comes in comparing a whole vector.
ئەگەر Python دەزانیت: لەوێ values[0] یەکەم و پارچەی values[1:4] پێش 4 دەوەستێت (ئیندێکسەکان، پارچەکان). لە MATLABـدا v(1) یەکەم و v(2:4) 4 دەگرێتەوە. هەڵبژاردنی بە مەرجێک، وەک v(v > 5)، پێویستی بە هەیە و لە بەراوردکردنی هەموو ئاڕاستەبڕێک دێت.
Practiceڕاهێنان
Hana's quiz marks are m = [64 71 58 90 83 77 95 60]. Show the first mark, the last mark (without counting), marks 2 to 4, and the first and last together.
نمرەکانی تاقیکردنەوەی هانا m = [64 71 58 90 83 77 95 60]ـن. یەکەم نمرە، دوایین نمرە (بێ ژماردن)، نمرەکانی 2 تا 4 و یەکەم و دوایین پێکەوە پیشان بدە.
Show answerوەڵامەکە پیشان بدە
m = [64 71 58 90 83 77 95 60];
m(1)
m(end)
m(2:4)
m([1 end])ans =
64
ans =
60
ans =
71 58 90
ans =
64 60Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 5
endalso works inside an index vector.endلەناو کار دەکات.
For A = [6 1 8 3; 4 9 2 7; 5 0 11 12], write A(5), A(10) and A(12) as row–column pairs and give their values.
بۆ A = [6 1 8 3; 4 9 2 7; 5 0 11 12]، A(5)، A(10) و A(12) وەک جووتی ڕیز–ستوون بنووسە و بدە.
Show answerوەڵامەکە پیشان بدە
Columns hold 3 elements each. A(5) is A(2,2), which is 9; A(10) is A(1,4), which is 3; A(12) is A(3,4), which is 12 (the last element).
هەر ستوونێک 3 هەڵدەگرێت. A(5) هەمان A(2,2)ـە کە 9ـە؛ A(10) هەمان A(1,4)ـە کە 3ـە؛ A(12) هەمان A(3,4)ـە کە 12ـە (دوایین ).
Start with B = eye(2). Predict B after each line, then check: B(3,3) = 5, then B(:,2) = [], then B(1,:) = [].
بە B = eye(2) دەست پێبکە. B دوای هەر دێڕێک پێشبینی بکە، پاشان بیپشکنە: B(3,3) = 5، پاشان B(:,2) = []، پاشان B(1,:) = [].
Show answerوەڵامەکە پیشان بدە
B = eye(2);
B(3,3) = 5
B(:,2) = []
B(1,:) = []B =
1 0 0
0 1 0
0 0 5
B =
1 0
0 0
0 5
B =
0 0
0 5Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 2
grows to ; the new places are 0.
دەبێتە ؛ شوێنە نوێیەکان 0ن.
- Lineدێڕی 3-4
.
.
With an index vector you can reorder columns. For T = [1 2 3; 4 5 6], swap columns 1 and 3 in one command.
بە دەتوانیت ڕیزبەندیی ستوونەکان بگۆڕیت. بۆ T = [1 2 3; 4 5 6]، ستوونەکانی 1 و 3 بە یەک بگۆڕەوە.
Show answerوەڵامەکە پیشان بدە
T = [1 2 3; 4 5 6];
T = T(:, [3 2 1])T =
3 2 1
6 5 4Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 2
All rows, columns in the order 3, 2, 1; the result is stored back in
T.هەموو ڕیزەکان، ستوونەکان بە ڕیزبەندیی 3، 2، 1؛ ئەنجامەکە دیسان لە
Tـدا هەڵدەگیرێتەوە.
Summaryپوختە
v(k)is element k;A(r,c)is row r, column c. MATLAB counts from 1;endis the last index.- An index must be a positive whole number; reading outside the array is an error.
- One index on a matrix is a linear index, counted down the columns.
:means all or from–to:A(:,n),A(n,:),A(m:n,p:q); index vectors pick chosen elements.- Assigning outside the size grows the array and pads with zeros.
= []deletes elements of a vector, or whole rows or columns of a matrix.
v(k)kـیە؛A(r,c)ڕیزی r، ستوونی cـیە. MATLAB لە 1ـەوە دەژمێرێت؛endدوایین .- دەبێت موجەب بێت؛ خوێندنەوە لە دەرەوەی .
- یەک لەسەر کە بە درێژایی ستوونەکان دەژمێردرێت.
:واتە هەموو یان لە–تا:A(:,n)،A(n,:)،A(m:n,p:q)؛ دیاریکراو هەڵدەبژێرن.- دانانی لە دەرەوەی قەبارەکە گەورە دەکات و بە سفر پڕی دەکاتەوە.
= []، یان ڕیز و ستوونی تەواوی دەسڕێتەوە.
What is the difference between A(2,1) and A(1,2)?جیاوازیی نێوان A(2,1) و A(1,2) چییە؟
A(2,1) is row 2, column 1; A(1,2) is row 1, column 2 — usually different elements.
A(2,1) ڕیزی 2، ستوونی 1ـە؛ A(1,2) ڕیزی 1، ستوونی 2ـە — زۆربەی جار دوو جیاوازن.
How do you get the last column of any matrix?چۆن دوایین ستوونی هەر ماتریکسێک وەردەگریت؟
A(:, end)
A(:, end)
Why does A(2,2) = [] fail on a matrix?بۆچی A(2,2) = [] لەسەر ماتریکسێک سەرناکەوێت؟
Removing one element would leave a hole; a matrix can only lose whole rows or columns.
لابردنی یەک کونێک بەجێ دەهێڵێت؛ تەنها دەتوانێت ڕیز یان ستوونی تەواو لەدەست بدات.
Videoڤیدیۆ
Watch a videoسەیری ڤیدیۆیەک بکە
A video that viewers liked a lot and praised in their comments, chosen for this lesson.ڤیدیۆیەک کە بینەران زۆریان پێ خۆش بووە و لە کۆمێنتەکانیاندا ستایشیان کردووە، بۆ ئەم وانەیە هەڵبژێردراوە.
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 24:54-31:16: A(2,2), one index on a vector, counting from 1 (A(0) is an error), end and end-10, changing one element, a whole row with the colon, and 1:end.
لە 24:54 تا 31:16 سەیر بکە: A(2,2)، یەک لەسەر ، ژماردن لە یەکەوە (A(0) )، end و end-10، گۆڕینی یەک ، ڕیزێکی تەواو بە : و 1:end.
Lesson complete!وانەکە تەواو بوو!
Well done. Review the key terms, then go on to the next lesson.دەستخۆش. سەیرێکی زاراوە سەرەکییەکان بکەرەوە، پاشان بڕۆ بۆ وانەی داهاتوو.
Key termsزاراوە سەرەکییەکان
Functions for Arraysکردارەکانی ڕیزەک
Related lecturesوانە پەیوەندیدارەکان
- Vectors and Rangesئاڕاستەبڕەکان و زنجیرەکان
- Functions for Arraysکردارەکانی ڕیزەک
- Matrix Operations vs Element-by-Element Operationsکارەکانی ماتریکس بەرامبەر کارەکانی توخم بە توخم
- Making Decisions: Relational Operators and if / elseif / elseبڕیاردان: کارپێکەرەکانی بەراوردکردن و if / elseif / else
- Matrix Notation and Special Matricesنووسینی ماتریکس و جۆرە تایبەتەکانی
- Lists: Creating and Indexingلیستەکان: دروستکردن و ئیندێکسەکان
References and further readingسەرچاوەکان و خوێندنەوەی زیاتر
This lecture is a first edition. The Kurdish text and terminology are waiting for review by Kurdish IT teachers.ئەم وانەیە چاپی یەکەمە. دەقە کوردییەکە و زاراوەکانی چاوەڕێی پێداچوونەوەی مامۆستایانی کوردی بواری IT ن.