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.
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, andsize(A,1)orsize(A,2)gives only one of them;numel(A)gives the number of elements ( 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)ژمارەی دەدات ( بۆ )؛length(A)گەورەترین ڕەهەند دەدات — بۆ ئەوە ژمارەی ، بەڵام بۆ ژمارەی ڕیزەکان نییە.
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])ans =
3 5
m =
3
n =
5
ans =
15
ans =
5
ans =
5Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 3
Two output variables in square brackets on the left: rows go to
m, columns ton.دوو لەناو کەوانەی چوارگۆشەدا لە لای چەپ: ژمارەی ڕیز دەچێتە
m، ستوون دەچێتەn.- Lineدێڕی 5
5, because the largest dimension is the 5 columns.
5، چونکە گەورەترین ڕەهەند 5 ستوونەکەیە.
- Lineدێڕی 6
For a vector,
lengthis simply its number of elements.بۆ ،
lengthتەنها ژمارەی .
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).
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 matrix is three numbers, not one.
لەسەر ئەم یەک ژمارە دەدەن: کۆ، لێکدان، گەورەترین، بچووکترین، تێکڕا. لەسەر بە درێژایی هەر ستوونێک کار دەکەن و ڕیزێک دەدەن کە بۆ هەر ستوونێک یەک وەڵامی تێدایە. ئەمە باوترین سەرسوڕمانی تازەکارانە: sumـی سێ ژمارەیە، نەک یەک.
sum(S,2) works along the rows بە درێژایی ستوونەکان کار دەکەن؛ sum(S,2) بە درێژایی ڕیزەکانCêITS = [3 8 2; 5 1 6];
sum(S)
prod(S)
max(S)
min(S)
mean(S)ans =
8 9 8
ans =
15 8 12
ans =
5 8 6
ans =
3 1 2
ans =
4.0000 4.5000 4.0000Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 2
, , .
، ، .
- Lineدێڕی 3
, , .
، ، .
- Lineدێڕی 6
The averages of the columns; 4.5 makes MATLAB show all three with decimals.
تێکڕای ستوونەکان؛ 4.5 وا دەکات MATLAB هەر سێکیان بە ژمارەی دوای پیشان بدات.
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(:) هەموو لە یەک ستووندا.
sum(S,2)
sum(S,'all')
sum(S(:))
mean(S,2)ans =
13
12
ans =
25
ans =
25
ans =
4.3333
4.0000Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 1
Row totals: and .
کۆی ڕیزەکان: و .
- Lineدێڕی 4
The same dimension argument works for
mean,prod,max(S,[],2)and others.هەمان ڕەهەند بۆ
mean،prod،max(S,[],2)و ئەوانی تر کار دەکات.
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) گەورەترینی ڕیزەکان — []ـی بەتاڵ جێگەی دووەم دەگرێت کە لێرەدا بەکار نایەت.
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)m =
25
k =
3
s =
3
j =
6
ans =
8 9 7
ans =
9
8Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 2
25 appears at positions 3 and 5;
maxreports the first, 3.25 لە شوێنەکانی 3 و 5دا هەیە؛
maxیەکەمیان دەڵێت، 3.- Lineدێڕی 5-6
Column maxima as a row; row maxima as a column.
گەورەترینی ستوونەکان وەک ڕیزێک؛ گەورەترینی ڕیزەکان وەک ستوونێک.
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 بە درێژایی ستوونەکانی . پشتیان لە کۆرسی دەگوترێتەوە: ناوەند، ناوەڕاستە و باو، لادانی پێوانەیی.
q = [14 18 11 20 15 18];
mean(q)
median(q)
mode(q)
std(q)
std(q, 1)ans =
16
ans =
16.5000
ans =
18
ans =
3.2863
ans =
3Explain 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
stddivides by (the sample formula) by default — MathWorks: std.stdبە شێوەی بنەڕەت بە دابەش دەکات ( ) — MathWorks: std.- Lineدێڕی 6
A second argument 1 divides by n: the population formula.
دووەمی 1 بە n دابەش دەکات: .
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 هەر ستوونێک بە جیا ڕێک دەخات — ڕیزەکان پێکەوە نامێننەوە.
w = [31 12 45 7 23];
s = sort(w)
sort(w, 'descend')
[s, k] = sort(w);
k
sort([4 -1; 2 7; -3 5])s =
7 12 23 31 45
ans =
45 31 23 12 7
k =
4 2 5 1 3
ans =
-3 -1
2 5
4 7Explain 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 () and column 2 () are sorted separately.
ستوونی 1 () و ستوونی 2 () بە جیا ڕێک دەخرێن.
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 لەسەر خشتەش ستوون بە ستوون کار دەکەن.
Diagonals, flips and turnsتیرە، وەرگێڕان و سووڕاندن
A few functions pick out or move whole parts of a matrix:
چەند بەشی تەواوی هەڵدەبژێرن یان دەجووڵێنن:
| Command | What 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 ( 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.' (جێگۆڕکێی ماتریکس) |
D = [2 7 1; 8 3 9; 4 6 5];
diag(D)
diag([4 1 6])
trace(D)
fliplr(D)
rot90(D)
tril(D)ans =
2
3
5
ans =
4 0 0
0 1 0
0 0 6
ans =
10
ans =
1 7 2
9 3 8
5 6 4
ans =
1 9 5
7 3 6
2 8 4
ans =
2 0 0
8 3 0
4 6 5Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 2
2, 3 and 5 sit on the main diagonal.
2، 3 و 5 لەسەر .
- 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))سێگۆشەیەکی خوارەوەی یەکەکانە.
A check with sums: magic(4)پشکنین بە کۆ: magic(4)
magic(n) builds a : an matrix of the numbers 1 to in which every row, every column and both diagonals have the same sum (for ). 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) دروست دەکات: لە ژمارەکانی 1 تا کە هەموو ڕیزێک، هەموو ستوونێک و هەردوو تیرەکەی هەمان کۆیان هەیە (بۆ ). تاقیکردنەوەیەکی باشە بۆ سەرەوە: 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.](/_astro/matlab-array-functions-magic-sums.en.cnqvjY0Q.gif)
![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.](/_astro/matlab-array-functions-magic-sums.ckb.BxNDWfvr.gif)
magic(4) all add up to 34ڕیز، ستوون و هەردوو تیرەی magic(4) هەموویان کۆیان 34ـەCêITM = magic(4)
sum(M)
sum(M,2)'
sum(diag(M))
sum(diag(fliplr(M)))M =
16 2 3 13
5 11 10 8
9 7 6 12
4 14 15 1
ans =
34 34 34 34
ans =
34 34 34 34
ans =
34
ans =
34Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 3
The row sums form a column;
'shows them as a row to save space.کۆی ڕیزەکان ستوونێک دروست دەکەن؛
'وەک ڕیز پیشانیان دەدات بۆ پاشەکەوتی شوێن.- Lineدێڕی 5
fliplrturns 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 : for that is , and for magic(3) it is 15.
کۆی هەموو هێڵێک ـە: بۆ دەکاتە و بۆ magic(3) دەکاتە 15.
More array commandsفەرمانەکانی زیاتری ڕیزەک
The course's commands list names a few more array commands. You will meet some of them again later:
لیستی وانەکە چەند تری ناو دەبات. هەندێکیان دواتر دووبارە دەبینیتەوە:
| Command | Exampleنموونە | 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 to , like linspace on a log scale (linspace)1 10 100 — n خاڵ لە تا ، وەک linspace بە پێوەری (linspace) |
dot(a, b) | dot([3 1 2], [4 5 1]) | 19 — the : 19 — ئەمە : |
cross(a, b) | cross([1 0 0], [0 1 0]) | 0 0 1 — the of two 3-element vectors0 0 1 — دوو سێ توخمی |
pinv(A) | (no example here)(لێرەدا بێ نموونە) | a “pseudo-inverse” that also exists for non-square matrices (beyond this course)«نیمچە پێچەوانە»یەک کە بۆ ناچوارگۆشەییش هەیە (لە دەرەوەی ئەم کۆرسەیە) |
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:
چوار وەڵامی پرسیارە گەورەکانی دەربارەی دەدەنەوە. ماتماتیکی پشت هەر یەکێکیان لە کۆرسی کالکولەسدا دەگوترێتەوە؛ لێرەدا تەنها پێویستە بزانیت هەر یەکەیان وەڵامی کام پرسیار دەداتەوە:
| Command | Question it answersوەڵامی کام پرسیار دەداتەوە | Mathsماتماتیک |
|---|---|---|
det(A) | the : one number; 0 means A has no inverse: یەک ژمارە؛ 0 واتە A پێچەوانەی نییە | Determinantsوانەی دیتەرمینانتەکان |
inv(A) | the , with ، بە مەرجی | The 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وانەی لابردنی گاوس–جۆردان |
K = [4 7; 2 6];
det(K)
inv(K)
rank(K)
rref(K)ans =
10
ans =
0.6000 -0.7000
-0.2000 0.4000
ans =
2
ans =
1 0
0 1Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 2
بە دەست: (دیتەرمینانتی ).
- Lineدێڕی 3
Check:
K*inv(K)gives the identity matrix. To solve equations,K\bis better thaninv(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)، بۆیە دەگاتە .
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 — کۆمپیوتەر چۆن دیتەرمینانت دەدۆزێتەوە) کار دەکات و بچووک دەمێنێتەوە.
A = [1 2 3; 4 5 6; 7 8 9];
det(A)ans = 6.6613e-16
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 2
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.لە جیاتی 0: . ژمارە وردەکان لە نێوان وەشانەکان، کۆمپیوتەرەکان و Octaveـدا دەکرێت جیاواز بن (numpyـی Python
-9.5162e-16دەدات)، بەڵام هەرگیز 3 نییە.
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ـیش پیشان دەدات کەی ڕیزەکان سەربەخۆ نین.
rank(A)
rcond(A)
rref(A)ans =
2
ans =
1.5420e-18
ans =
1.0000 0 -1.0000
0 1.0000 2.0000
0 0 0Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 1
Only two independent rows.
تەنها دوو ڕیزی سەربەخۆ.
- Lineدێڕی 2
rcondis close to 1 for a healthy matrix and close to 0 for a (nearly) singular one: says “singular”.rcondبۆ تەندروست نزیکی 1ـە و بۆ (یان نزیک لەوە) نزیکی 0ـە: دەڵێت «».- Lineدێڕی 3
The row of zeros at the bottom is the sign of a singular matrix. Round-off can also make MATLAB print
-1.0000and2.0000instead of and 2.ڕیزە سفرەکەی خوارەوە نیشانەی . دەتوانێت وا بکات MATLAB لە جیاتی و 2،
-1.0000و2.0000بنووسێت.
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) == 0 | Round-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ڕاهێنان
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وەڵامەکە پیشان بدە
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)ans =
260 270 295 275 325
ans =
605
460
360
ans =
1425
best =
150
105
90
day =
5
2
5
ans =
121
92
72Explain 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.
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وەڵامەکە پیشان بدە
marks = [72 95 64 88];
[s, k] = sort(marks, 'descend')s =
95 88 72 64
k =
2 4 1 3Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 2
Positions 2, 4, 1, 3: Shilan, Dilan, Aram, Karwan.
شوێنەکانی 2، 4، 1، 3: شیلان، دیلان، ئارام، کاروان.
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.
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وەڵامەکە پیشان بدە
M = magic(5);
sum(M)
sum(M,2)'
[sum(diag(M)) sum(diag(fliplr(M)))]ans =
65 65 65 65 65
ans =
65 65 65 65 65
ans =
65 65For 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وەڵامەکە پیشان بدە
B = [2 4; 1 2];
det(B)
rank(B)
rcond(B)ans =
0
ans =
1
ans =
0Explain 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 ;length(A)gives the largest dimension (not the number of rows).sum,prod,max,min,mean,median,mode,stdwork 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,crossrearrange or combine arrays.magic(n): equal row, column and diagonal sums — a good test ofsumanddiag.det,inv,rank,rrefanswer the linear-algebra questions; test singularity withrcondorrank, neverdet == 0.
size(A)دەکاتە[m n]؛numel(A)دەکاتە ؛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.
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: , . Use sum(A,'all') for 10.
لەسەر sum بە درێژایی هەر ستوونێک کار دەکات: ، . بۆ 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 is left. The true determinant is 0.
بە هەژمار دەکات کە نزیک کراونەتەوە؛ نزیکەی دەمێنێتەوە. ڕاستەقینە 0ـە.
Lesson complete!وانەکە تەواو بوو!
Well done. Review the key terms, then go on to the next lesson.دەستخۆش. سەیرێکی زاراوە سەرەکییەکان بکەرەوە، پاشان بڕۆ بۆ وانەی داهاتوو.
Key termsزاراوە سەرەکییەکان
Matrix Operations vs Element-by-Element Operationsکارەکانی ماتریکس بەرامبەر کارەکانی توخم بە توخم
Related lecturesوانە پەیوەندیدارەکان
- Creating Matricesدروستکردنی ماتریکسەکان
- Matrix Operations vs Element-by-Element Operationsکارەکانی ماتریکس بەرامبەر کارەکانی توخم بە توخم
- Determinants, Minors and Cofactorsدیتەرمینانتەکان، ماینەرەکان و کۆفاکتەرەکان
- Properties of Determinantsتایبەتمەندییەکانی دیتەرمینانت
- The Inverse of a Matrixپێچەوانەی ماتریکس
- Finding the Inverse by Row Operations (Gauss–Jordan)دۆزینەوەی پێچەوانە بە کارە سەرەتاییەکانی ڕیز (لابردنی گاوس–جۆردان)
- The Median and the Modeناوەڕاستە و باو
- Variance and Standard Deviationلێکنەچوون و لادانی پێوانەیی
- The Arithmetic Mean and the Weighted Meanناوەند و ناوەندی قورسکراو
- sum, max, min and Sortingsum، max، min و ڕیزکردن
References and further readingسەرچاوەکان و خوێندنەوەی زیاتر
This lecture is a first edition. The Kurdish text and terminology are waiting for review by Kurdish IT teachers.ئەم وانەیە چاپی یەکەمە. دەقە کوردییەکە و زاراوەکانی چاوەڕێی پێداچوونەوەی مامۆستایانی کوردی بواری IT ن.