1 -1 1
0 -1 0 -
1 0 1
Endi shu amalni algoritmi bilan tanishib chiqamiz:
>> for i=1:3; for j=1:3; C(j,i)=A(j,3-i+1); end; end; C
C =
1 0 -1
-1 0
-1 1
5 – misol: Berilgan matritsani soat strelkasiga qarshi 900 ga burish uchun ishlatiladigan rot90(A) komandasi:
>> A=[-1 0 1; 0 -1 0; 1 -1 1]
A =
-1 0 1
-1 0
-1 1
>> rot90(A) ans =
1 0 1
0 -1 -1 -
1 0 1
Endi shu amalning bajarilish tartibi ya’ni algoritmi haqida:
>> for i=1:3; for j=1:3; C(i,j)=A(j,3-i+1); end; end; C
C =
1 0 1
-1 -1
0 1
Undan tashqari matlabda maxsus ko’rinishdagi matritsalarni hosil qilish imkoniyati bor. Ana shunday matritsalarni hosil qiluvchi komandalarni kyeltirib o’tamiz:
>> A=[-1 0 1; 0 -1 0; 1 -1 1]
A =
-1 0 1
-1 0
-1 1
>> size(A) ans =
3 3
>> length(A)
ans = 3
>> ndims(A)
ans =
2
>> isempty(A) ans =
0
6 - misol: diag(A) komandasi berilgan matritsaning diagonalida tugan elementlarni ekranga chiqaradi:
>> A=[-1 0 1; 0 -1 0; 1 -1 1]
A =
-1 0 1
-1 0
-1 1
>> diag(A) ans =
-1 -1 1
Shu amalni o’zimiz bajarib chqamiz:
>> for i=1:3; D(i)=A(i,i);end; D
D =
-1 -1 1
7 – misol: eye(n) komandasi birlik matritsa hosil qilish:
>> eye(5) ans =
1 0 0 0 0
0 1 0 0 0
0 0 1 0 0
0 0 0 1 0
0 0 0 1
Endi shu matritsani m-faylga funksiyasini yaratamiz:
Ushbu m-faylga birlik matritsa hosil qiladigan protsedura yasadik va uning nomini diagonal.m deb nomladik. Endi bu m-fayl yordamida diagonal(n) komandasi hosil bo’ldi. Endi ushbu komanda yordamida ham eye(n) komandasining bajargan ishini bajarsa bo’ladi: >> diagonal(5) ans =
0 0 0 0