MATLABMATLAB · Chapterبەش 4 · Lectureوانە 4.4

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

Measure arrays with size, numel and length (the largest dimension), use sum, prod, max, min, mean, median, mode and std down the columns or along the rows, sort with positions, rearrange with diag, trace, fliplr, rot90 and tril, check a magic square, meet reshape, cat, find, logspace, dot and cross, and see what det, inv, rank and rref answer — including why a singular matrix's det is a tiny number, not 0.

بە size، numel و length (گەورەترین ڕەهەند) بپێوە، sum، prod، max، min، mean، median، mode و std بە درێژایی ستوون یان ڕیز بەکار بهێنە، لەگەڵ شوێنەکان ڕێکیان بخە، بە diag، trace، fliplr، ⁦rot90⁩ و tril ڕێکیان بخەرەوە، بپشکنە، reshape، cat، find، logspace، dot و cross بناسە و ببینە det، inv، rank و rref وەڵامی چی دەدەنەوە — هەروەها بۆچی detـی ژمارەیەکی زۆر بچووکە، نەک 0.

15 min read16 خولەک خوێندنەوە · 16 stepsهەنگاو

What you will learnچی فێر دەبیت

  • Use size, numel and length correctly, knowing that length is the largest dimension.
  • Apply sum, prod, max, min, mean, median, mode and std to columns, rows or a whole matrix.
  • Sort vectors and matrices and get positions with a second output.
  • Rearrange matrices with diag, fliplr, rot90 and tril, and check a magic square with sums.
  • Say what det, inv, rank and rref answer and test singularity with rcond instead of det == 0.
  • size، numel و length بە دروستی بەکار بهێنیت و بزانیت length گەورەترین ڕەهەندە.
  • sum، prod، max، min، mean، median، mode و std لەسەر ستوون، ڕیز یان هەموو بەکار بهێنیت.
  • و ڕێک بخەیت و بە دووەم شوێنەکان وەربگریت.
  • بە diag، fliplr، ⁦rot90⁩ و tril ڕێک بخەیتەوە و بە کۆ بپشکنیت.
  • بڵێیت det، inv، rank و rref وەڵامی چی دەدەنەوە و لە جیاتی ⁦det == 0⁩ بە rcond بپشکنیت.

How big is it? size, numel, lengthچەند گەورەیە؟ size، numel، length

Three measure an array:

سێ دەپێون:

  • size(A) gives the number of rows and of columns as a row [m n]; [m, n] = size(A) stores them in two variables, and size(A,1) or size(A,2) gives only one of them;
  • numel(A) gives the number of elements (m×nm \times n for a matrix);
  • length(A) gives the largest dimension — for a vector that is its number of elements, but for a matrix it is not the number of rows.
  • size(A) ژمارەی ڕیز و ستوون وەک ڕیزێکی [m n] دەدات؛ [m, n] = size(A) لە دوو هەڵیاندەگرێت و size(A,1) یان size(A,2) تەنها یەکێکیان دەدات؛
  • numel(A) ژمارەی دەدات (m×nm \times n بۆ )؛
  • length(A) گەورەترین ڕەهەند دەدات — بۆ ئەوە ژمارەی ، بەڵام بۆ ژمارەی ڕیزەکان نییە.
A 3 × 5 grid with its 3 rows and 5 columns labelled. Three cards beside it: size(T) gives 3 5, length(T) gives 5 (the larger of 3 and 5), numel(T) gives 15 (3 × 5).تۆڕێکی 3 × 5 کە 3 ڕیز و 5 ستوونەکەی نیشانە کراون. سێ کارت لە تەنیشتیەوە: size(T) دەکاتە 3 5، length(T) دەکاتە 5 (گەورەترینی 3 و 5)، numel(T) دەکاتە 15 (3 × 5).
Three questions about one 3×53 \times 5 matrixسێ پرسیار دەربارەی یەک 3×53 \times 5CêIT
MATLABType in a terminalلە تێرمیناڵدا بینووسە
T = [4 8 15 16 23; 42 7 1 9 30; 11 5 27 3 18];
size(T)
[m, n] = size(T)
numel(T)
length(T)
length([7 3 9 1 6])
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 3

Two output variables in square brackets on the left: rows go to m, columns to n.

دوو لەناو کەوانەی چوارگۆشەدا لە لای چەپ: ژمارەی ڕیز دەچێتە m، ستوون دەچێتە n.

Lineدێڕی 5

5, because the largest dimension is the 5 columns.

5، چونکە گەورەترین ڕەهەند 5 ستوونەکەیە.

Lineدێڕی 6

For a vector, length is simply its number of elements.

بۆ ، length تەنها ژمارەی .

Quick checkپشکنینی خێراB = zeros(6,2). What does length(B) give?B = zeros(6,2). length(B) چی دەدات؟

Working with Matrices in MatlabChristopher Lum · Englishئینگلیزی · 146K viewsبینین · 2.3K likesلایک · Partبەشی 27:30–29:03 (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 27:30-29:03: length of a vector and [rows, cols] = size(A) of a matrix.

لە ⁦27:30⁩ تا ⁦29:03⁩ سەیر بکە: درێژیی بە length و قەبارەی بە [rows, cols] = size(A).

How big is it? size, numel, lengthچەند گەورەیە؟ size، numel، length

length is not the number of rowslength ژمارەی ڕیز نییە

Because length returns the larger dimension, it can surprise you: length(zeros(3,8)) is 8, but length(zeros(8,3)) is also 8. When you need the number of rows, ask for it directly with size(A,1); for the number of columns, size(A,2).

چونکە length ڕەهەندە گەورەکە دەگەڕێنێتەوە، دەتوانێت سەرت سوڕ بهێنێت: length(zeros(3,8)) 8ـە، بەڵام length(zeros(8,3))ـیش 8ـە. کاتێک ژمارەی ڕیزەکانت پێویستە، ڕاستەوخۆ بە size(A,1) داوای بکە؛ بۆ ژمارەی ستوونەکان، size(A,2).

Quick checkپشکنینی خێراYou want the number of rows of a matrix R. Which command is always right?ژمارەی ڕیزەکانی بە ناوی Rـت دەوێت. کام هەمیشە ڕاستە؟

Maths down the columns: sum, prod, max, min, meanماتماتیک بە درێژایی ستوونەکان: sum، prod، max، min، mean

On a vector these functions give one number: the total, the product, the largest, the smallest, the average. On a matrix they work down each column and give a row with one answer per column. This is the most common surprise for beginners: sum of a 2×32 \times 3 matrix is three numbers, not one.

لەسەر ئەم یەک ژمارە دەدەن: کۆ، لێکدان، گەورەترین، بچووکترین، تێکڕا. لەسەر بە درێژایی هەر ستوونێک کار دەکەن و ڕیزێک دەدەن کە بۆ هەر ستوونێک یەک وەڵامی تێدایە. ئەمە باوترین سەرسوڕمانی تازەکارانە: sumـی 2×32 \times 3 سێ ژمارەیە، نەک یەک.

The 2 × 3 matrix S = [3 8 2; 5 1 6]. An arrow runs down each column into a result row: sum gives 8 9 8, max gives 5 8 6 and mean gives 4 4.5 4. A separate arrow along each row, labelled sum(S,2), gives the column 13; 12.ماتریکسی S = [3 8 2; 5 1 6] بە قەبارەی 2 × 3. تیرێک بە درێژایی هەر ستوونێک دادەبەزێت بۆ ڕیزێکی ئەنجام: sum دەکاتە 8 9 8، max دەکاتە 5 8 6 و mean دەکاتە 4 4.5 4. تیرێکی جیا بە درێژایی هەر ڕیزێک، بە ناوی sum(S,2)، ستوونی 13؛ 12 دەدات.
Matrix functions work down the columns; sum(S,2) works along the rows بە درێژایی ستوونەکان کار دەکەن؛ sum(S,2) بە درێژایی ڕیزەکانCêIT
Column by column (new numbers)ستوون بە ستوون (بە ژمارەی نوێ)MATLABType in a terminalلە تێرمیناڵدا بینووسە
S = [3 8 2; 5 1 6];
sum(S)
prod(S)
max(S)
min(S)
mean(S)
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 2

3+53 + 5, 8+18 + 1, 2+62 + 6.

3+53 + 5، 8+18 + 1، 2+62 + 6.

Lineدێڕی 3

3×53 \times 5, 8×18 \times 1, 2×62 \times 6.

3×53 \times 5، 8×18 \times 1، 2×62 \times 6.

Lineدێڕی 6

The averages of the columns; 4.5 makes MATLAB show all three with decimals.

تێکڕای ستوونەکان؛ ⁦4.5⁩ وا دەکات MATLAB هەر سێکیان بە ژمارەی دوای پیشان بدات.

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

Maths down the columns: sum, prod, max, min, meanماتماتیک بە درێژایی ستوونەکان: sum، prod، max، min، mean

Along the rows, or everything: sum(A,2), sum(A,'all')بە درێژایی ڕیزەکان، یان هەمووی: sum(A,2)، sum(A,'all')

A second chooses the direction: dimension 1 is down the columns (the default), dimension 2 is along the rows. sum(A,2) gives a column of row totals. sum(A,'all') adds every element and gives one number (MATLAB R2018b or newer; the current Octave manual lists it too); sum(A(:)) does the same in every version, because A(:) is all elements in one column.

دووەم ئاڕاستەکە هەڵدەبژێرێت: ڕەهەندی 1 بە درێژایی ستوونەکانە (بنەڕەت)، ڕەهەندی 2 بە درێژایی ڕیزەکان. sum(A,2) ستوونێکی کۆی ڕیزەکان دەدات. sum(A,'all') هەموو کۆ دەکاتەوە و یەک ژمارە دەدات (⁦MATLAB R2018b⁩ یان نوێتر؛ ڕێبەری ئێستای Octaveـیش ناوی دەبات)؛ sum(A(:)) لە هەموو وەشانێکدا هەمان کار دەکات، چونکە A(:) هەموو لە یەک ستووندا.

MATLABType in a terminalلە تێرمیناڵدا بینووسە
sum(S,2)
sum(S,'all')
sum(S(:))
mean(S,2)
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 1

Row totals: 3+8+2=133 + 8 + 2 = 13 and 5+1+6=125 + 1 + 6 = 12.

کۆی ڕیزەکان: 3+8+2=133 + 8 + 2 = 13 و 5+1+6=125 + 1 + 6 = 12.

Lineدێڕی 4

The same dimension argument works for mean, prod, max(S,[],2) and others.

هەمان ڕەهەند بۆ mean، prod، max(S,[],2) و ئەوانی تر کار دەکات.

Quick checkپشکنینی خێراA table W has one row per shop and one column per day. Which command gives each shop's weekly total?خشتەی W بۆ هەر دووکانێک یەک ڕیز و بۆ هەر ڕۆژێک یەک ستوونی هەیە. کام کۆی هەفتانەی هەر دووکانێک دەدات؟

Maths down the columns: sum, prod, max, min, meanماتماتیک بە درێژایی ستوونەکان: sum، prod، max، min، mean

Where is the largest? [m, k] = max(v)گەورەترین لە کوێیە؟ [m, k] = max(v)

Ask max (or min) for two outputs and the second one is the position of the answer. If the largest value appears twice, you get the first position. On a matrix, max(X,[],1) is the column maxima (the same as max(X)) and max(X,[],2) the row maxima — the empty [] stands in the place of a second array, which is not used here.

داوای دوو لە max (یان min) بکە و دووەمیان شوێنی وەڵامەکەیە. ئەگەر گەورەترین دوو جار دەرکەوێت، یەکەم شوێن وەردەگریت. لەسەر ، max(X,[],1) گەورەترینی ستوونەکانە (هەمان max(X)) و max(X,[],2) گەورەترینی ڕیزەکان — []ـی بەتاڵ جێگەی دووەم دەگرێت کە لێرەدا بەکار نایەت.

MATLABType in a terminalلە تێرمیناڵدا بینووسە
v = [12 7 25 9 25 3];
[m, k] = max(v)
[s, j] = min(v)
X = [5 9 1; 8 2 7];
max(X,[],1)
max(X,[],2)
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 2

25 appears at positions 3 and 5; max reports the first, 3.

⁦25⁩ لە شوێنەکانی 3 و 5دا هەیە؛ max یەکەمیان دەڵێت، 3.

Lineدێڕی 5-6

Column maxima as a row; row maxima as a column.

گەورەترینی ستوونەکان وەک ڕیزێک؛ گەورەترینی ڕیزەکان وەک ستوونێک.

Quick checkپشکنینی خێراp = [40 15 60 15]. What does [s, j] = min(p) give?p = [40 15 60 15]. [s, j] = min(p) چی دەدات؟

MATLAB Crash Course for BeginnersfreeCodeCamp.org · Englishئینگلیزی · 1.7M viewsبینین · 37K likesلایک · Partبەشی 41:50–44:40 (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 41:50-44:40: help max, then [M, I] = max(y) gives the largest value and its position, and x(I) finds where it happens.

لە ⁦41:50⁩ تا ⁦44:40⁩ سەیر بکە: help max، پاشان [M, I] = max(y) گەورەترین و شوێنەکەی دەدات و x(I) دەدۆزێتەوە لە کوێدا ڕوودەدات.

Maths down the columns: sum, prod, max, min, meanماتماتیک بە درێژایی ستوونەکان: sum، prod، max، min، mean

The statistics family: mean, median, mode, stdخێزانی ئامار: mean، median، mode، std

The commands list of the course names the statistics functions mean, median and std; mode belongs with them. They work on vectors, and down the columns of a matrix, like sum. The statistics behind them is taught in the statistics course: the mean, the median and the mode, the standard deviation.

لیستی وانەکە mean، median و std ناو دەبات؛ modeـیش لەگەڵیانە. لەسەر کار دەکەن و وەک sum بە درێژایی ستوونەکانی . پشتیان لە کۆرسی دەگوترێتەوە: ناوەند، ناوەڕاستە و باو، لادانی پێوانەیی.

Six quiz marks out of 20شەش نمرەی تاقیکردنەوە لە ⁦20⁩MATLABType in a terminalلە تێرمیناڵدا بینووسە
q = [14 18 11 20 15 18];
mean(q)
median(q)
mode(q)
std(q)
std(q, 1)
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 3

The middle of 11 14 15 18 18 20 is halfway between 15 and 18.

ناوەڕاستی ⁦11 14 15 18 18 20⁩ لە نێوان ⁦15⁩ و ⁦18⁩دایە.

Lineدێڕی 4

18 appears most often.

⁦18⁩ زۆرترین جار دەردەکەوێت.

Lineدێڕی 5

std divides by n−1n - 1 (the sample formula) by default — MathWorks: std.

std بە شێوەی بنەڕەت بە n−1n - 1 دابەش دەکات ( ) — MathWorks: std.

Lineدێڕی 6

A second argument 1 divides by n: the population formula.

دووەمی 1 بە n دابەش دەکات: .

Quick checkپشکنینی خێراWhich call gives the population standard deviation (dividing by n) of q?کام بانگکردن بۆ (دابەشکردن بە n) لە qـدا دەدات؟

Sorting: sortڕیزکردن: sort

sort(v) puts a vector in . sort(v,'descend') gives . With two outputs, [s, k] = sort(v), the second output k tells where each sorted value came from. On a matrix, sort sorts each column on its own — the rows do not stay together.

sort(v) بە ڕێک دەخات. sort(v,'descend') دەدات. بە دوو ، [s, k] = sort(v)، دووەم k دەڵێت هەر ڕیزکراو لە کوێوە هاتووە. لەسەر ، sort هەر ستوونێک بە جیا ڕێک دەخات — ڕیزەکان پێکەوە نامێننەوە.

MATLABType in a terminalلە تێرمیناڵدا بینووسە
w = [31 12 45 7 23];
s = sort(w)
sort(w, 'descend')
[s, k] = sort(w);
k
sort([4 -1; 2 7; -3 5])
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 5

The smallest value (7) was element 4 of w, the next (12) element 2, and so on.

بچووکترین ⁦(7)⁩ 4ی w بوو، دواتر ⁦(12)⁩ 2 و هتد.

Lineدێڕی 6

Column 1 (4,2,−34, 2, -3) and column 2 (−1,7,5-1, 7, 5) are sorted separately.

ستوونی 1 (4,2,−34, 2, -3) و ستوونی 2 (−1,7,5-1, 7, 5) بە جیا ڕێک دەخرێن.

If you know Python: sorted(values) and sum, max, min work on one list there (sum, max and min); MATLAB's versions also work column by column on a table.

ئەگەر Python دەزانیت: sorted(values) و sum، max، min لەوێ لەسەر یەک لیست کار دەکەن (sum، max و min)؛ وەشانەکانی MATLAB لەسەر خشتەش ستوون بە ستوون کار دەکەن.

Quick checkپشکنینی خێراWhat is sort([5 1; 3 8])?sort([5 1; 3 8]) چییە؟

Diagonals, flips and turnsتیرە، وەرگێڕان و سووڕاندن

A few functions pick out or move whole parts of a matrix:

چەند بەشی تەواوی هەڵدەبژێرن یان دەجووڵێنن:

CommandWhat it givesچی دەدات
diag(A)the of A as a column (top-left to bottom-right) A وەک ستوونێک (لە سەرەوەی چەپەوە بۆ خوارەوەی ڕاست)
diag(v)a with the vector v on its main diagonal and zeros elsewhere (a diagonal matrix) کە v لەسەر و سفر لە شوێنەکانی تردایە ( تیرەیی)
trace(A)the : the sum of the main diagonal, the same as sum(diag(A)): کۆی ، هەمان sum(diag(A))
fliplr(A)A mirrored left–right (flipud mirrors up–down)A بە ئاوێنەیی چەپ–ڕاست (flipud سەرەوە–خوارەوە)
rot90(A, k)A turned k quarter-turns anticlockwise (k=1k = 1 if left out)A بە k چارەکە سووڕانەوەی پێچەوانەی ئاڕاستەی کاتژمێر (ئەگەر k نەنووسرێت 1ـە)
tril(A), triu(A)the lower or upper triangle of A; the other places become 0سێگۆشەی خوارەوە یان سەرەوەی A؛ شوێنەکانی تر دەبنە 0
transpose(A)the function form of A.' (the transpose)شێوەی A.' (جێگۆڕکێی ماتریکس)
MATLABType in a terminalلە تێرمیناڵدا بینووسە
D = [2 7 1; 8 3 9; 4 6 5];
diag(D)
diag([4 1 6])
trace(D)
fliplr(D)
rot90(D)
tril(D)
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 2

2, 3 and 5 sit on the main diagonal.

2، 3 و 5 لەسەر .

Lineدێڕی 4

2+3+5=102 + 3 + 5 = 10 (the trace).

کۆ: 2+3+5=102 + 3 + 5 = 10 (کۆی تیرە).

Lineدێڕی 6

One quarter-turn anticlockwise: the last column (1, 9, 5) is now the top row. This is a real turn, not a transpose (a mirror, not a turn).

یەک چارەکە سووڕانەوەی پێچەوانەی کاتژمێر: دوایین ستوون (⁦1⁩، 9، ⁦5⁩) ئێستا ڕیزی سەرەوەیە. ئەمە ڕاستەقینەیە، نەک (ئاوێنە، نەک سووڕانەوە).

Lineدێڕی 7

Everything above the main diagonal became 0. tril(ones(4)) is a handy lower triangle of ones.

هەموو شتێک لە سەرووی بوو بە 0. tril(ones(4)) سێگۆشەیەکی خوارەوەی یەکەکانە.

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

A check with sums: magic(4)پشکنین بە کۆ: magic(4)

magic(n) builds a : an n×nn \times n matrix of the numbers 1 to n2n^2 in which every row, every column and both diagonals have the same sum (for n≥3n \ge 3). It is a good test of the functions above: sum(M) adds the columns, sum(M,2) the rows, sum(diag(M)) the main diagonal and sum(diag(fliplr(M))) the other diagonal.

magic(n) دروست دەکات: n×nn \times n لە ژمارەکانی 1 تا n2n^2 کە هەموو ڕیزێک، هەموو ستوونێک و هەردوو تیرەکەی هەمان کۆیان هەیە (بۆ n≥3n \ge 3). تاقیکردنەوەیەکی باشە بۆ سەرەوە: sum(M) ستوونەکان کۆ دەکاتەوە، sum(M,2) ڕیزەکان، sum(diag(M)) و sum(diag(fliplr(M))) تیرەکەی تر.

Animation of M = magic(4) = [16 2 3 13; 5 11 10 8; 9 7 6 12; 4 14 15 1]. First every row lights up with its sum 34, then every column, then the main diagonal (16, 11, 6, 1) and the other diagonal (13, 10, 7, 4); the last frame says: all sums are 34.
Animation of M = magic(4) = [16 2 3 13; 5 11 10 8; 9 7 6 12; 4 14 15 1]. First every row lights up with its sum 34, then every column, then the main diagonal (16, 11, 6, 1) and the other diagonal (13, 10, 7, 4); the last frame says: all sums are 34.
Rows, columns and both diagonals of magic(4) all add up to 34ڕیز، ستوون و هەردوو تیرەی magic(4) هەموویان کۆیان ⁦34⁩ـەCêIT
MATLABType in a terminalلە تێرمیناڵدا بینووسە
M = magic(4)
sum(M)
sum(M,2)'
sum(diag(M))
sum(diag(fliplr(M)))
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 3

The row sums form a column; ' shows them as a row to save space.

کۆی ڕیزەکان ستوونێک دروست دەکەن؛ ' وەک ڕیز پیشانیان دەدات بۆ پاشەکەوتی شوێن.

Lineدێڕی 5

fliplr turns the other diagonal (13, 10, 7, 4) into the main diagonal of the flipped matrix.

fliplr تیرەکەی تر (⁦13⁩، ⁦10⁩، 7، ⁦4⁩) دەکات بە وەرگێڕدراوەکە.

Every line adds up to n(n2+1)2\frac{n(n^2+1)}{2}: for n=4n = 4 that is 4⋅172=34\frac{4 \cdot 17}{2} = 34, and for magic(3) it is 15.

کۆی هەموو هێڵێک n(n2+1)2\frac{n(n^2+1)}{2}ـە: بۆ n=4n = 4 دەکاتە 4⋅172=34\frac{4 \cdot 17}{2} = 34 و بۆ magic(3) دەکاتە ⁦15⁩.

Quick checkپشکنینی خێراWhich command adds up the numbers on the main diagonal of M?کام ژمارەکانی سەر M کۆ دەکاتەوە؟

More array commandsفەرمانەکانی زیاتری ڕیزەک

The course's commands list names a few more array commands. You will meet some of them again later:

لیستی وانەکە چەند تری ناو دەبات. هەندێکیان دواتر دووبارە دەبینیتەوە:

CommandExampleنموونەResultئەنجام
reshape(v, m, n)reshape(1:6, 2, 3)[1 3 5; 2 4 6] — the same elements in a new shape, filled down the columns⁦[1 3 5; 2 4 6]⁩ — هەمان بە شێوەیەکی نوێ، ستوون بە ستوون پڕ کراوە
cat(dim, A, B)cat(1, A, B)the same as [A; B]; cat(2, A, B) is [A B] (joining)هەمان [A; B]؛ cat(2, A, B) هەمان [A B]ـە (پێکەوەلکاندن)
find(v)find([0 3 0 8])2 4 — the positions of the nonzero elements (more in comparing a whole vector)⁦2 4⁩ — شوێنی ئەو سفر نین (زیاتر لە بەراوردکردنی هەموو ئاڕاستەبڕێک)
logspace(a, b, n)logspace(0, 2, 3)1 10 100 — n points from 10a10^a to 10b10^b, like linspace on a log scale (linspace)⁦1 10 100 — n⁩ خاڵ لە 10a10^a تا 10b10^b، وەک linspace بە پێوەری (linspace)
dot(a, b)dot([3 1 2], [4 5 1])19 — the : 3⋅4+1⋅5+2⋅13 \cdot 4 + 1 \cdot 5 + 2 \cdot 1⁦19⁩ — ئەمە : 3⋅4+1⋅5+2⋅13 \cdot 4 + 1 \cdot 5 + 2 \cdot 1
cross(a, b)cross([1 0 0], [0 1 0])0 0 1 — the of two 3-element vectors⁦0 0 1⁩ — دوو سێ توخمی
pinv(A)(no example here)(لێرەدا بێ نموونە)a “pseudo-inverse” that also exists for non-square matrices (beyond this course)«نیمچە پێچەوانە»یەک کە بۆ ناچوارگۆشەییش هەیە (لە دەرەوەی ئەم کۆرسەیە)
Quick checkپشکنینی خێراWhat does reshape(1:6, 3, 2) give?reshape(1:6, 3, 2) چی دەدات؟

Linear algebra in one word: det, inv, rank, rrefجەبری هێڵی بە یەک وشە: det، inv، rank، rref

Four commands answer the big questions of about a square matrix. The maths behind each is taught in the calculus course; here you only need to know which question each one answers:

چوار وەڵامی پرسیارە گەورەکانی دەربارەی دەدەنەوە. ماتماتیکی پشت هەر یەکێکیان لە کۆرسی کالکولەسدا دەگوترێتەوە؛ لێرەدا تەنها پێویستە بزانیت هەر یەکەیان وەڵامی کام پرسیار دەداتەوە:

CommandQuestion it answersوەڵامی کام پرسیار دەداتەوەMathsماتماتیک
det(A)the : one number; 0 means A has no inverse: یەک ژمارە؛ 0 واتە A پێچەوانەی نییەDeterminantsوانەی دیتەرمینانتەکان
inv(A)the A−1A^{-1}, with AA−1=IAA^{-1} = I A−1A^{-1}، بە مەرجی AA−1=IAA^{-1} = IThe inverse of a matrixوانەی پێچەوانەی ماتریکس
rank(A)the : how many rows are independent (not built from the other rows): چەند ڕیز سەربەخۆن (لە ڕیزەکانی ترەوە دروست نەکراون)not taught in the calculus lessonsلە وانەکانی کالکولەسدا نییە
rref(A)A after : the simplest form row operations can reachA دوای : سادەترین شێوە کە پێی دەگەنGauss–Jordan eliminationوانەی لابردنی گاوس–جۆردان
MATLABType in a terminalلە تێرمیناڵدا بینووسە
K = [4 7; 2 6];
det(K)
inv(K)
rank(K)
rref(K)
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 2

4⋅6−7⋅2=104 \cdot 6 - 7 \cdot 2 = 10 (a 2×22 \times 2 determinant).

بە دەست: 4⋅6−7⋅2=104 \cdot 6 - 7 \cdot 2 = 10 (دیتەرمینانتی 2×22 \times 2).

Lineدێڕی 3

Check: K*inv(K) gives the identity matrix. To solve equations, K\b is better than inv(K)*b (left division).

پشکنین: K*inv(K) دەدات. بۆ چارەسەرکردنی ، K\b لە inv(K)*b باشترە (دابەشکردنی چەپ).

Lineدێڕی 4-5

Both rows are independent (rank 2), so Gauss–Jordan reaches the identity matrix.

هەردوو ڕیزەکە سەربەخۆن ( ⁦2⁩)، بۆیە دەگاتە .

Quick checkپشکنینی خێراWhich command tells you how many rows of a matrix are independent?کام پێت دەڵێت چەند ڕیزی سەربەخۆن؟

Linear algebra in one word: det, inv, rank, rrefجەبری هێڵی بە یەک وشە: det، inv، rank، rref

A determinant that should be 0دیتەرمینانتێک کە دەبوو 0 بێت

The course notes compute det([1 2 3; 4 5 6; 7 8 9]) and print 3. That is wrong. In this matrix, row 3 minus row 2 is the same as row 2 minus row 1 (both are 3 3 3), so the rows are not independent, the matrix is a , and its determinant is exactly 0: subtract row 1 from row 2 and row 2 from row 3 and two rows become equal (two equal rows give 0). But MATLAB does not print 0 either: det works with rounded through row operations (LU — how computers find determinants), and a tiny is left over.

تێبینییەکانی وانەکە det([1 2 3; 4 5 6; 7 8 9]) هەژمار دەکەن و 3 دەنووسن. ئەمە هەڵەیە. لەم جیاوازیی ڕیزی 3 و ڕیزی 2 هەمان جیاوازیی ڕیزی 2 و ڕیزی 1ـە (هەردووکیان ⁦3 3 3⁩ـن)، بۆیە ڕیزەکان سەربەخۆ نین، و ڕێک 0ـە: ڕیزی 1 لە ڕیزی 2 و ڕیزی 2 لە ڕیزی 3 دەربکە و دوو ڕیز یەکسان دەبن (دوو ڕیزی یەکسان 0 دەدەن). بەڵام ⁦MATLAB 0⁩ـیش نانووسێت: det بە و (LU — کۆمپیوتەر چۆن دیتەرمینانت دەدۆزێتەوە) کار دەکات و بچووک دەمێنێتەوە.

MATLABType in a terminalلە تێرمیناڵدا بینووسە
A = [1 2 3; 4 5 6; 7 8 9];
det(A)
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 2

6.6613×10−166.6613 \times 10^{-16} instead of 0: round-off. The exact digits can differ between releases, computers and Octave (Python's numpy gives -9.5162e-16), but it is never 3.

6.6613×10−166.6613 \times 10^{-16} لە جیاتی 0: . ژمارە وردەکان لە نێوان وەشانەکان، کۆمپیوتەرەکان و Octaveـدا دەکرێت جیاواز بن (numpyـی Python -9.5162e-16 دەدات)، بەڵام هەرگیز 3 نییە.

Quick checkپشکنینی خێراWhat is the exact determinant of [1 2 3; 4 5 6; 7 8 9]? ڕاستەقینەی [1 2 3; 4 5 6; 7 8 9] چەندە؟

Linear algebra in one word: det, inv, rank, rrefجەبری هێڵی بە یەک وشە: det، inv، rank، rref

Test with rcond or rank, not det == 0بە rcond یان rank بپشکنە، نەک بە det == 0

So how do you check whether a matrix is singular? Not with det(A) == 0: as you just saw, round-off hides the 0. MathWorks recommends rcond (close to 1 for a healthy matrix, close to 0 for a nearly singular one) or cond; rank also shows when the rows are not independent.

کەواتە چۆن دەزانیت ؟ بە det(A) == 0 نا: وەک بینیت، سفرەکە دەشارێتەوە. MathWorks rcond پێشنیار دەکات (بۆ تەندروست نزیکی 1ـە و بۆ کە نزیکە لە نزیکی 0ـە) یان cond؛ rankـیش پیشان دەدات کەی ڕیزەکان سەربەخۆ نین.

MATLABType in a terminalلە تێرمیناڵدا بینووسە
rank(A)
rcond(A)
rref(A)
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 1

Only two independent rows.

تەنها دوو ڕیزی سەربەخۆ.

Lineدێڕی 2

rcond is close to 1 for a healthy matrix and close to 0 for a (nearly) singular one: 1.5×10−181.5 \times 10^{-18} says “singular”.

rcond بۆ تەندروست نزیکی 1ـە و بۆ (یان نزیک لەوە) نزیکی 0ـە: 1.5×10−181.5 \times 10^{-18} دەڵێت «».

Lineدێڕی 3

The row of zeros at the bottom is the sign of a singular matrix. Round-off can also make MATLAB print -1.0000 and 2.0000 instead of −1-1 and 2.

ڕیزە سفرەکەی خوارەوە نیشانەی . دەتوانێت وا بکات MATLAB لە جیاتی −1-1 و 2، -1.0000 و 2.0000 بنووسێت.

Quick checkپشکنینی خێراdet(B) shows -1.2e-15. What is the best conclusion?det(B) -1.2e-15 پیشان دەدات. باشترین دەرەنجام چییە؟

Common mistakesهەڵە باوەکان

MistakeهەڵەWhat happensچی ڕوودەداتFixچارەسەر
Expecting sum(A) of a matrix to be one numberچاوەڕوانیی ئەوەی sum(A)ـی یەک ژمارە بێتA row of column sumsڕیزێک لە کۆی ستوونەکانsum(A,'all') or sum(A(:)) for the total; sum(A,2) for row sums.sum(A,'all') یان sum(A(:)) بۆ کۆی گشتی؛ sum(A,2) بۆ کۆی ڕیزەکان.
length(A) for the number of rowslength(A) بۆ ژمارەی ڕیزەکانThe largest dimension, which may be the columnsگەورەترین ڕەهەند، کە لەوانەیە ستوونەکان بنsize(A,1)size(A,1)
Capital letters: Sum(A), Det(A)پیتی گەورە: Sum(A)، Det(A)An error: unknown function: نەناسراوsum, det — MATLAB is case-sensitive.sum، det — MATLAB .
A variable called sum or max: sum = 10; بە ناوی sum یان max: sum = 10;Later sum([1 2]) is read as indexing into your variable and failsدواتر sum([1 2]) وەک لە دەخوێنرێتەوە و سەرناکەوێتChoose another name (total) and remove the bad one with clear sum.ناوێکی تر هەڵبژێرە (total) و خراپەکە بە clear sum لاببە.
Testing det(A) == 0تاقیکردنەوەی det(A) == 0Round-off makes singular matrices look non-singular وا پیشان دەدات کە پێچەوانەیان هەبێتUse rcond(A) (near 0 means singular) or rank(A).rcond(A) (نزیکی 0 واتە ) یان rank(A) بەکار بهێنە.
rot(A) to turn a matrixrot(A) بۆ An error: there is no rot: rot بوونی نییەrot90(A) (anticlockwise), rot90(A,-1) (clockwise)rot90(A) (پێچەوانەی کاتژمێر)، rot90(A,-1) (ئاڕاستەی کاتژمێر)
Expecting sort on a matrix to keep rows togetherچاوەڕوانیی ئەوەی sort لەسەر ڕیزەکان پێکەوە بهێڵێتەوەEvery column is sorted separatelyهەر ستوونێک بە جیا ڕێک دەخرێتUse sortrows(A, c) to sort whole rows by column c.sortrows(A, c) بەکار بهێنە بۆ ڕیزی تەواو بە پێی ستوونی c.

Practiceڕاهێنان

ExerciseڕاهێنانA bakery's weekهەفتەیەکی نانەواخانەیەک

A bakery has three branches (rows) and records the loaves sold on five days (columns): W = [120 95 130 110 150; 80 105 90 100 85; 60 70 75 65 90]. Find (a) each day's total, (b) each branch's total, (c) the grand total, (d) each branch's best day and its sales, (e) each branch's average.

نانەواخانەیەک سێ لقی هەیە (ڕیز) و ژمارەی ئەو نانانەی لە پێنج ڕۆژدا فرۆشراون تۆمار دەکات (ستوون): W = [120 95 130 110 150; 80 105 90 100 85; 60 70 75 65 90]. بیدۆزەرەوە (أ) کۆی هەر ڕۆژێک، (ب) کۆی هەر لقێک، (ج) کۆی گشتی، (د) باشترین ڕۆژی هەر لقێک و فرۆشەکەی، (هـ) هەر لقێک.

Show answerوەڵامەکە پیشان بدە
MATLABType in a terminalلە تێرمیناڵدا بینووسە
W = [120 95 130 110 150; 80 105 90 100 85; 60 70 75 65 90];
sum(W)
sum(W,2)
sum(W,'all')
[best, day] = max(W,[],2)
mean(W,2)
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 5

Row maxima with their positions: branch 1 sold most on day 5, branch 2 on day 2, branch 3 on day 5.

گەورەترینی ڕیزەکان لەگەڵ شوێنەکانیان: لقی 1 لە ڕۆژی 5دا زۆرترینی فرۆشت، لقی 2 لە ڕۆژی 2، لقی 3 لە ڕۆژی 5.

ExerciseڕاهێنانWho came first?کێ یەکەم بوو؟

Aram, Shilan, Karwan and Dilan scored marks = [72 95 64 88]. Sort the marks from highest to lowest and use the second output of sort to name the order.

ئارام، شیلان، کاروان و دیلان ئەم نمرانەیان هێنا: marks = [72 95 64 88]. نمرەکان لە بەرزترینەوە بۆ نزمترین ڕێک بخە و دووەمی sort بەکار بهێنە بۆ ناوهێنانی ڕیزبەندییەکە.

Show answerوەڵامەکە پیشان بدە
MATLABType in a terminalلە تێرمیناڵدا بینووسە
marks = [72 95 64 88];
[s, k] = sort(marks, 'descend')
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 2

Positions 2, 4, 1, 3: Shilan, Dilan, Aram, Karwan.

شوێنەکانی 2، 4، 1، 3: شیلان، دیلان، ئارام، کاروان.

ExerciseڕاهێنانMeasure itبیپێوە

For Z = zeros(4,7) predict size(Z), numel(Z), length(Z) and size(Z,1), then check.

بۆ Z = zeros(4,7) پێشبینیی size(Z)، numel(Z)، length(Z) و size(Z,1) بکە، پاشان بیپشکنە.

Show answerوەڵامەکە پیشان بدە

size(Z) → 4 7; numel(Z) → 28; length(Z) → 7 (the larger dimension); size(Z,1) → 4.

size(Z) دەکاتە ⁦4 7⁩؛ numel(Z) دەکاتە ⁦28⁩؛ length(Z) دەکاتە 7 (ڕەهەندە گەورەکە)؛ size(Z,1) دەکاتە 4.

ExerciseڕاهێنانA 5×55 \times 5 magic squareچوارگۆشەیەکی جادوویی 5×55 \times 5

What should every line of magic(5) add up to? Check the columns, the rows and both diagonals in MATLAB.

کۆی هەموو هێڵێکی magic(5) دەبێت چەند بێت؟ ستوونەکان، ڕیزەکان و هەردوو تیرەکە لە MATLABـدا بپشکنە.

Show answerوەڵامەکە پیشان بدە
n(n2+1)2=5⋅262=65\frac{n(n^2+1)}{2} = \frac{5 \cdot 26}{2} = 65
MATLABType in a terminalلە تێرمیناڵدا بینووسە
M = magic(5);
sum(M)
sum(M,2)'
[sum(diag(M)) sum(diag(fliplr(M)))]
ExerciseڕاهێنانIs it singular?بێپێچەوانەیە؟

For B = [2 4; 1 2] find det(B), rank(B) and rcond(B). Does B have an inverse?

بۆ B = [2 4; 1 2]، det(B)، rank(B) و rcond(B) بدۆزەرەوە. ئایا B پێچەوانەی هەیە؟

Show answerوەڵامەکە پیشان بدە
MATLABType in a terminalلە تێرمیناڵدا بینووسە
B = [2 4; 1 2];
det(B)
rank(B)
rcond(B)
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 2-4

Row 1 is twice row 2, so B is singular: no inverse. Here the row operations happen to be exact, so MATLAB shows a true 0.

ڕیزی 1 دوو جارەی ڕیزی 2ـیە، بۆیە B : پێچەوانەی نییە. لێرەدا بە ڕێکەوت ورد دەردەچن، بۆیە ⁦MATLAB 0⁩ـی ڕاستەقینە پیشان دەدات.

Summaryپوختە

  • size(A) gives [m n]; numel(A) gives m⋅nm \cdot n; length(A) gives the largest dimension (not the number of rows).
  • sum, prod, max, min, mean, median, mode, std work down the columns of a matrix; sum(A,2) along the rows; sum(A,'all') everything.
  • [m, k] = max(v) and [s, k] = sort(v) also give positions; sort(v,'descend') sorts from largest.
  • diag, trace, fliplr, rot90, tril, triu, reshape, cat, dot, cross rearrange or combine arrays.
  • magic(n): equal row, column and diagonal sums — a good test of sum and diag.
  • det, inv, rank, rref answer the linear-algebra questions; test singularity with rcond or rank, never det == 0.
  • size(A) دەکاتە [m n]؛ numel(A) دەکاتە m⋅nm \cdot n؛ length(A) گەورەترین ڕەهەندە (نەک ژمارەی ڕیزەکان).
  • sum، prod، max، min، mean، median، mode، std بە درێژایی ستوونەکانی کار دەکەن؛ sum(A,2) بە درێژایی ڕیزەکان؛ sum(A,'all') هەمووی.
  • [m, k] = max(v) و [s, k] = sort(v) شوێنەکانیش دەدەن؛ sort(v,'descend') لە گەورەترینەوە ڕێک دەخات.
  • diag، trace، fliplr، rot90، tril، triu، reshape، cat، dot، cross ڕێکدەخەنەوە یان تێکەڵ دەکەن.
  • magic(n): کۆی یەکسانی ڕیز، ستوون و تیرە — تاقیکردنەوەیەکی باش بۆ sum و diag.
  • det، inv، rank، rref وەڵامی پرسیارەکانی دەدەنەوە؛ بە rcond یان rank بپشکنە، هەرگیز بە det == 0.
Review questionsپرسیارەکانی پێداچوونەوە
Why is sum([1 2; 3 4]) equal to [4 6] and not 10?بۆچی sum([1 2; 3 4]) یەکسانە بە [4 6] و نەک ⁦10⁩؟

On a matrix sum works down each column: 1+3=41 + 3 = 4, 2+4=62 + 4 = 6. Use sum(A,'all') for 10.

لەسەر sum بە درێژایی هەر ستوونێک کار دەکات: 1+3=41 + 3 = 4، 2+4=62 + 4 = 6. بۆ ⁦10⁩ sum(A,'all') بەکار بهێنە.

What is length(zeros(2,9))?length(zeros(2,9)) چییە؟

9, the larger dimension.

9، ڕەهەندە گەورەکە.

Why does MATLAB not show 0 for det([1 2 3; 4 5 6; 7 8 9])?بۆچی MATLAB بۆ det([1 2 3; 4 5 6; 7 8 9]) 0 پیشان نادات؟

It computes with rounded ; a round-off of about 10−1610^{-16} is left. The true determinant is 0.

بە هەژمار دەکات کە نزیک کراونەتەوە؛ نزیکەی 10−1610^{-16} دەمێنێتەوە. ڕاستەقینە 0ـە.

Lesson complete!وانەکە تەواو بوو!

Well done. Review the key terms, then go on to the next lesson.دەستخۆش. سەیرێکی زاراوە سەرەکییەکان بکەرەوە، پاشان بڕۆ بۆ وانەی داهاتوو.

Key termsزاراوە سەرەکییەکان

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

Matrix Operations vs Element-by-Element Operationsکارەکانی ماتریکس بەرامبەر کارەکانی توخم بە توخم

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

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