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 دوایین )، و بە درێژایی ستوونەکان تێبگە، بە دوو خاڵ و ڕیز، ستوون و پارچە وەربگرە، بە دانانی لە دەرەوەیان گەورە بکە (پڕکردنەوە بە سفر) و بە ⁦[]⁩ ، ڕیز و ستوون بسڕەوە.

15 min read15 خولەک خوێندنەوە · 18 stepsهەنگاو

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 a23a_{23} in maths (the address of an entry).

چەندین لەژێر یەک ناودا هەڵدەگرێت (بۆچی ئاڕاستەبڕەکان). بۆ گەیشتن بە یەکێکیان، شوێنەکەی — — لەناو کەوانەی بازنەییدا لە دوای ناوەکە بنووسە: jars(3) سێیەم . لە دوو دەدەیت، سەرەتا ڕیز، پاشان ستوون: A(2,3) ئەو کە لە ڕیزی 2 و ستوونی 3دایە — هەمان ڕیزبەندیی a23a_{23} لە ماتماتیکدا (ناونیشانی ژمارەیەک).

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⁩ـیشدا کار دەکات.

One element of a vector and of a matrixیەک توخم لە ئاڕاستەبڕێک و لە ماتریکسێکMATLABType in a terminalلە تێرمیناڵدا بینووسە
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)
Explain 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, but A(3,2) (row 3, column 2) is 0.

ڕیزبەندی گرنگە: A(2,3) (ڕیزی 2، ستوونی ⁦3) 2⁩ـە، بەڵام A(3,2) (ڕیزی 3، ستوونی ⁦2) 0⁩ـە.

Quick checkپشکنینی خێراIn A = [6 1 8 3; 4 9 2 7; 5 0 11 12], what is A(1,4)?لە A = [6 1 8 3; 4 9 2 7; 5 0 11 12]ـدا، A(1,4) چییە؟

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.

MATLABType in a terminalلە تێرمیناڵدا بینووسە
jars(end)
jars(end-1)
A(end,1)
A(2,end)
A(end,end)
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 3-5

end can stand for the last row, the last column, or both.

end دەتوانێت جێگەی دوایین ڕیز، دوایین ستوون یان هەردووکیان بگرێتەوە.

Quick checkپشکنینی خێراv = [8 3 6 1 9]. What is v(end-2)?v = [8 3 6 1 9]. v(end-2) چییە؟

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.

بە دەتوانێت لە لای چەپی = بوەستێت: نوێ جێگەی کۆنەکە دەگرێتەوە و باقیی وەک خۆی دەمێنێتەوە.

MATLABType in a terminalلە تێرمیناڵدا بینووسە
jars(2) = 120
A(3,1) = 10
Quick checkپشکنینی خێراAfter A(2,2) = 0, what changes in a 3×33 \times 3 matrix A?دوای A(2,2) = 0، چی لە 3×33 \times 3 بە ناوی A دەگۆڕێت؟

Index 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, or jars(7) when jars has 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) کاتێک jars 6 هەیە — دەڵێت لە دەرەوەی سنووری .
MATLABType in a terminalلە تێرمیناڵدا بینووسە
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 — write v(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.)

لە دەرەوەی کاتێک دەخوێنیتەوە: . (نووسین لە دەرەوە جیاوازە: گەورە دەکات — لە خوارەوە ببینە.)

Quick checkپشکنینی خێراv has 5 elements. Which command does NOT give an error?v 5 هەیە. کام نادات؟

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 3×43 \times 4 matrix, A(6) is the 6th element counted this way: row 3 of column 2.

MATLAB ستوون بە ستوون لە هەڵدەگرێت: سەرەتا هەموو ستوونی 1، پاشان ستوونی 2 و هتد. بۆیە دەتوانیت بە یەک ژمارەش بگەیتە ، بە ، کە بە درێژایی ستوونەکان دەژمێردرێت. لە 3×43 \times 4ـی ئێمەدا، A(6) شەشەم بەم ژماردنە: ڕیزی 3ی ستوونی 2.

The 3 × 4 matrix A = [6 1 8 3; 4 9 2 7; 5 0 11 12] drawn as a grid. Each cell shows its value and, in a small corner, its linear index: 1, 2, 3 down column 1, then 4, 5, 6 down column 2, 7, 8, 9 down column 3 and 10, 11, 12 down column 4. A path runs down each column in turn. The cell with linear index 6 (value 0, row 3, column 2) is highlighted: A(6) is the same element as A(3,2).ماتریکسی A = [6 1 8 3; 4 9 2 7; 5 0 11 12] بە قەبارەی 3 × 4 وەک تۆڕێک. هەر خانەیەک نرخەکەی و لە گۆشەیەکی بچووکدا ئیندێکسی هێڵییەکەی پیشان دەدات: 1، 2، 3 بە درێژایی ستوونی 1، پاشان 4، 5، 6 بە درێژایی ستوونی 2، 7، 8، 9 بە درێژایی ستوونی 3 و 10، 11، 12 بە درێژایی ستوونی 4. ڕێڕەوێک یەک لە دوای یەک بە ناو ستوونەکاندا دادەبەزێت. ئەو خانەیەی ئیندێکسی هێڵییەکەی 6ـە (نرخ 0، ڕیزی 3، ستوونی 2) ڕووناک کراوەتەوە: A(6) هەمان توخمی A(3,2)ـیە.
Linear indexes run down the columns: A(6) is A(3,2) بە درێژایی ستوونەکان دەڕۆن: A(6) هەمان A(3,2)ـەCêIT
MATLABType in a terminalلە تێرمیناڵدا بینووسە
A = [6 1 8 3; 4 9 2 7; 5 0 11 12];
A(6)
A(8)
A(:)'
Explain 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) is A(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(:) هەموو بە ڕیزبەندیی هەڵگرتن وەک یەک ستوونی درێژ لیست دەکات؛ ' تەنها دەیکات بە ڕیز بۆ پاشەکەوتی شوێن.

Quick checkپشکنینی خێراB = [1 2; 3 4]. What is B(2)?B = [1 2; 3 4]. B(2) چییە؟

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) سێ دوایینەکەن.

MATLABType in a terminalلە تێرمیناڵدا بینووسە
v = [7 3 15 9 21 4 12 18 6];
u = v(3:7)
v(end-2:end)
Explain 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.

لە سێیەم لە کۆتاییەوە تا دوایین .

Quick checkپشکنینی خێراv = [7 3 15 9 21 4 12 18 6]. How many elements does v(2:5) have?v = [7 3 15 9 21 4 12 18 6]. v(2:5) چەند هەیە؟

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⁩ بەو ڕیزبەندییە هەڵدەبژێرێت.

MATLABType in a terminalلە تێرمیناڵدا بینووسە
w = 5:4:45
w([2, 4, 8:11])
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 1

11 elements from 5 to 45 in steps of 4 (ranges).

⁦11⁩ لە 5 تا ⁦45⁩ بە هەنگاوی 4 (زنجیرەکان).

Lineدێڕی 2

The second, the fourth and the eighth to the eleventh elements.

دووەم، چوارەم و هەشتەم تا یازدەهەم .

Quick checkپشکنینی خێراk = [10 20 30 40 50]. What is k([5 1])?k = [10 20 30 40 50]. k([5 1]) چییە؟

Rows, 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:

لە ، دوو خاڵ لە جیاتی یەکێک لە واتە ڕیز یان ستوونی تەواو. بە زنجیرە لە هەردوو شوێنەکەدا پارچەیەکی لاکێشەیی دەبڕیت:

CommandMeaningواتا
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: پارچەیەک
The same 5 × 6 matrix N drawn four times. In the first copy column 3 is highlighted (N(:,3)); in the second, row 2 (N(2,:)); in the third, rows 2 to 4 (N(2:4,:)); in the fourth, the block of rows 1 to 3 and columns 2 to 4 (N(1:3,2:4)).هەمان ماتریکسی N بە قەبارەی 5 × 6 چوار جار کێشراوە. لە یەکەمیاندا ستوونی 3 ڕووناک کراوەتەوە (N(:,3))؛ لە دووەمدا ڕیزی 2 (N(2,:))؛ لە سێیەمدا ڕیزەکانی 2 تا 4 (N(2:4,:))؛ لە چوارەمدا پارچەی ڕیزەکانی 1 تا 3 و ستوونەکانی 2 تا 4 (N(1:3,2:4)).
A colon picks a whole row or column; two ranges cut out a blockدوو خاڵ ڕیز یان ستوونێکی تەواو هەڵدەبژێرێت؛ دوو زنجیرە پارچەیەک دەبڕنCêIT
The four pictures in code (a new 5×65 \times 6 matrix)چوار وێنەکە بە کۆد (ماتریکسێکی نوێی 5×65 \times 6)MATLABType in a terminalلە تێرمیناڵدا بینووسە
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)
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 2

Column 3 comes out as a column vector.

ستوونی 3 وەک ستوون دەردەچێت.

Lineدێڕی 5

Rows 1–3 and columns 2–4: a 3×33 \times 3 block.

ڕیزەکانی ⁦1–3⁩ و ستوونەکانی ⁦2–4⁩: پارچەیەکی 3×33 \times 3.

Quick checkپشکنینی خێراWhich command gives the last two columns of a 4×64 \times 6 matrix M?کام دوو ستوونی کۆتایی 4×64 \times 6 بە ناوی M دەدات؟

Working with Matrices in MatlabChristopher Lum · Englishئینگلیزی · 146K viewsبینین · 2.3K likesلایک · Partبەشی 14:17–18:14 (ofلە 31:02)

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 4×74 \times 7 matrix is built in one line and then read in parts:

ڕیزی دروستکراو لە زنجیرە و ئامادەکراو (دروستکردنی ماتریکسەکان) خشتەیەکی خێرا بۆ ڕاهێنان دەدەن. لێرەدا 4×74 \times 7 بە یەک دێڕ دروست دەکرێت و پاشان بەش بە بەش دەخوێنرێتەوە:

MATLABType in a terminalلە تێرمیناڵدا بینووسە
R = [9:-1:3; ones(1,7); 3:3:21; zeros(1,7)]
R(3, 2:4)
R(:, end)
R([1 3], [1 7])
Explain 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.

Quick checkپشکنینی خێراFor the matrix R above, what is R(2:3, 1)?بۆ Rـی سەرەوە، R(2:3, 1) چییە؟

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.

بۆ زیادکردنی ڕیزێک یان ستوونێک، بدە بە شوێنێک کە هێشتا بوونی نییە. بەشە نوێیەکە دەبێت بگونجێت: ڕیزێکی نوێ پێویستی بە هەمان ژمارە هەیە کە ستوونەکان هەن. تەنها یەک ژمارە لە لای ڕاست هەموو ڕیزە نوێیەکە پڕ دەکاتەوە.

MATLABType in a terminalلە تێرمیناڵدا بینووسە
G = [3 1 4 1; 5 9 2 6];
G(3,:) = 10:5:25
G(4,:) = 2
Explain 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ـی هەبێت (پێکەوەلکاندنی ماتریکسەکان).

Quick checkپشکنینی خێراG is 4×44 \times 4. What happens with G(5,:) = [1 2 3]?G 4×44 \times 4ـە. بە G(5,:) = [1 2 3] چی ڕوودەدات؟

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].
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].
Grow with AW(4,5) = 17, then delete columns with AW(:,2:4) = []گەورەکردن بە AW(4,5) = 17، پاشان سڕینەوەی ستوون بە AW(:,2:4) = []CêIT
MATLABType in a terminalلە تێرمیناڵدا بینووسە
AW = [3 6 9; 8 5 11];
AW(4,5) = 17
Q(2,5) = 9
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 2

AW was 2×32 \times 3; position (4,5) needs 4 rows and 5 columns. Everything new is 0 except the 17.

AW 2×32 \times 3 بوو؛ شوێنی ⁦(4,5)⁩ پێویستی بە 4 ڕیز و 5 ستوونە. هەموو شتێکی نوێ 0ـە جگە لە ⁦17⁩.

Lineدێڕی 3

Q did not exist (if it does, run clear Q first): MATLAB makes a 2×52 \times 5 matrix of zeros with 9 at (2,5).

Q بوونی نەبوو (ئەگەر هەیە، سەرەتا clear Q لێبدە): MATLAB 2×52 \times 5 لە سفر دروست دەکات و 9 لە ⁦(2,5)⁩دا دادەنێت.

A partial row works the same way: G(6,2:end) = 7 on the 4×44 \times 4 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ـی 4×44 \times 4 ڕیزی 5 پڕ لە سفر زیاد دەکات (کەس پێ نەداوە) و ڕیزی 6 = 0 7 7 7 — ئەو ستوونەی بازت داوە ئەویش 0ـە.

MATLABType in a terminalلە تێرمیناڵدا بینووسە
G(6,2:end) = 7
Quick checkپشکنینی خێراx = [4 5] and then x(4) = 8. What is x?x = [4 5] و پاشان x(4) = 8. x چییە؟

Adding 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).

وەک گەورە دەبێت. نووسین لە یەک شوێن دوای کۆتایی یەک زیاد دەکات؛ نووسین دوورتر سفر لە نێوانیاندا زیاد دەکات. ئەمە بەسوودە، بەڵام هەڵەیەکی نووسین لە دەتوانێت بە بێدەنگی زۆر گەورە دروست بکات — کاتێک ئەنجامەکان سەیر دەردەکەون قەبارەکە بپشکنە (پێوانەکردنی ڕیزەکەکان).

MATLABType in a terminalلە تێرمیناڵدا بینووسە
w = 1:3;
w(6) = 9
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 2

Places 4 and 5 were never given values, so they are 0.

شوێنەکانی 4 و 5 هەرگیز پێ نەدرا، بۆیە 0ن.

Quick checkپشکنینی خێراYou meant marks(5) = 70 but typed marks(50) = 70 on a 4-element vector. What happens?مەبەستت marks(5) = 70 بوو بەڵام لەسەر بە 4 marks(50) = 70ـت نووسی. چی ڕوودەدات؟

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 دەسڕێتەوە.

Deleting elements, then columns (new numbers)سڕینەوەی توخمەکان، پاشان ستوونەکان (بە ژمارەی نوێ)MATLABType in a terminalلە تێرمیناڵدا بینووسە
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) = []
Explain 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 3×23 \times 2 matrix.

ستوونەکانی 2، 3 و 4 نامێنن؛ ستوونەکانی 1 و 5 دێنە لای یەک و 3×23 \times 2 دروست دەکەن.

Quick checkپشکنینی خێراS is 3×53 \times 5. What is the size after S(1,:) = []?S 3×53 \times 5ـە. دوای S(1,:) = [] قەبارەکەی چەندە؟

Practice on one matrixڕاهێنان لەسەر یەک ماتریکس

Type this 4×54 \times 5 matrix and try each step before you open the answer:

ئەم 4×54 \times 5ـە بنووسە و پێش کردنەوەی وەڵامەکە هەر هەنگاوێک تاقی بکەرەوە:

MATLABType in a terminalلە تێرمیناڵدا بینووسە
P = [3 8 1 6 10; 7 2 9 4 5; 12 11 0 15 14; 6 13 16 2 8]
ExerciseڕاهێنانOne element, one column, two columnsیەک توخم، یەک ستوون، دوو ستوون

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وەڵامەکە پیشان بدە
MATLABType in a terminalلە تێرمیناڵدا بینووسە
P(3,4)
P(:,2)
P(:,2:3)
ExerciseڕاهێنانA block, then deleteپارچەیەک، پاشان سڕینەوە

(d) Show the 2×32 \times 3 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×32 \times 3ـی ڕیزەکانی ⁦2–3⁩ و ستوونەکانی ⁦3–5⁩ پیشان بدە. (هـ) ڕیزی 4 بسڕەوە. (و) پاشان ستوونی 5 بسڕەوە. لە کۆتاییدا P چ قەبارەیەکی هەیە؟

Show answerوەڵامەکە پیشان بدە
MATLABType in a terminalلە تێرمیناڵدا بینووسە
P(2:3,3:5)
P(4,:) = []
P(:,5) = []
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 3

4×5→3×5→3×44 \times 5 \to 3 \times 5 \to 3 \times 4.

4×5→3×5→3×44 \times 5 \to 3 \times 5 \to 3 \times 4.

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 errorDelete 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) = 5No 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ڕاهێنان

ExerciseڕاهێنانQuiz marksنمرەکانی تاقیکردنەوە

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وەڵامەکە پیشان بدە
MATLABType in a terminalلە تێرمیناڵدا بینووسە
m = [64 71 58 90 83 77 95 60];
m(1)
m(end)
m(2:4)
m([1 end])
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 5

end also works inside an index vector.

end لەناو کار دەکات.

ExerciseڕاهێنانLinear or row–column?ئیندێکسی هێڵی یان ڕیز–ستوون؟

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⁩ـە (دوایین ).

ExerciseڕاهێنانGrow and shrinkگەورەکردن و بچووککردنەوە

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وەڵامەکە پیشان بدە
MATLABType in a terminalلە تێرمیناڵدا بینووسە
B = eye(2);
B(3,3) = 5
B(:,2) = []
B(1,:) = []
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 2

2×22 \times 2 grows to 3×33 \times 3; the new places are 0.

2×22 \times 2 دەبێتە 3×33 \times 3؛ شوێنە نوێیەکان 0ن.

Lineدێڕی 3-4

3×3→3×2→2×23 \times 3 \to 3 \times 2 \to 2 \times 2.

3×3→3×2→2×23 \times 3 \to 3 \times 2 \to 2 \times 2.

ExerciseڕاهێنانSwap two columnsگۆڕینەوەی دوو ستوون

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وەڵامەکە پیشان بدە
MATLABType in a terminalلە تێرمیناڵدا بینووسە
T = [1 2 3; 4 5 6];
T = T(:, [3 2 1])
Explain 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; end is 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)؛ دیاریکراو هەڵدەبژێرن.
  • دانانی لە دەرەوەی قەبارەکە گەورە دەکات و بە سفر پڕی دەکاتەوە.
  • = [] ، یان ڕیز و ستوونی تەواوی دەسڕێتەوە.
Review questionsپرسیارەکانی پێداچوونەوە
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.ڤیدیۆیەک کە بینەران زۆریان پێ خۆش بووە و لە کۆمێنتەکانیاندا ستایشیان کردووە، بۆ ئەم وانەیە هەڵبژێردراوە.

MATLAB Crash Course for BeginnersfreeCodeCamp.org · Englishئینگلیزی · 1.7M viewsبینین · 37K likesلایک · Partبەشی 24:54–31:16 (ofلە 1:57:45)

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زاراوە سەرەکییەکان

Next lessonوانەی داهاتوو

Functions for Arraysکردارەکانی ڕیزەک

References and further readingسەرچاوەکان و خوێندنەوەی زیاتر

This lecture is a first edition. The Kurdish text and terminology are waiting for review by Kurdish IT teachers.ئەم وانەیە چاپی یەکەمە. دەقە کوردییەکە و زاراوەکانی چاوەڕێی پێداچوونەوەی مامۆستایانی کوردی بواری IT ن.