>> A=[-1 0 1; 0 -1 0; 1 -1 1]
A =
-1 0 1
0 -1 0
1 -1 1
>> rot90(A)
ans =
1 0 1
0 -1 -1
-1 0 1
Endi shu amalning bajarilish tartibi ya’ni algoritmini keltiramiz:
>> for i=1:3; for j=1:3; C(i,j)=A(j,3-i+1); end; end; C
C =
1 0 1
0 -1 -1
-1 0 1
Misol. MATLABda matritsalarni chapdan o’ngga burishda fliplr buyrug’idan foydalanish:
>> A=[-1 0 1; 0 -1 0; 1 -1 1]
A =
-1 0 1
0 -1 0
1 -1 1
>> fliplr(A)
ans =
1 0 -1
0 -1 0
1 -1 1
Undan tashqari MATLABda maxsus ko’rinishdagi matritsalarni hosil qilish
imkoniyati bor. Ana shunday matritsalarni hosil qiluvchi buyruqlarni keltirib
o’tamiz:
>> A=[-1 0 1; 0 -1 0; 1 -1 1]
A =
-1 0 1
0 -1 0
1 -1 1
>> size(A)
ans =
3 3 % ya`ni matritsaning o`lchami (3x3) o`lchamga ega bo`ladi.
>> length(A)
ans =
3
>> ndims(A)
ans = % oxirgi hosil bo`lgan matritsaning elemntlari sonini chiqarish.
2
>> isempty(A)
ans = 0 % matritsaning elementlari mavjud bo`lgani uchun 0 qiymat qaytaradi.
Misol: Berilgan A va B matritsalarni bir biriga ko’paytirish:
>> A=[-1 0 1; 0 -1 0; 1 -1 1]
A =
-1 0 1
0 -1 0
1 -1 1
>> B=[1 1 0; 2 -1 0; 3 0 1]
B =
1 1 0
2 -1 0
3 0 1
>> A*B
ans =
2 -1 1
-2 1 0
2 2 1
Endi shu amalni algoritmi haqida ya’ni o’z qo’limiz yordamida bajaramiz:
>> for i=1:3; for j=1:3; C(i,j)=0; for k=1:3; C(i,j)=C(i,j)+A(i,k)*B(k,j); end; end; end; C
C =
2 -1 1
-2 1 0
2 2 1
diag(A) buyrug’i berilgan matritsaning diagonalida tugan elementlarni ekranga
chiqaradi:
>> A=[-1 0 1; 0 -1 0; 1 -1 1]
A =
-1 0 1
0 -1 0
1 -1 1
>> diag(A)
ans =
-1 % A matritsaning dioganal elementlarini ustun ko`rinishida chiqaradi.
-1
1
1
Shu amalni o’zimiz bajarib chqamiz:
>> for i=1:3; D(i)=A(i,i);end; D
D =
-1 -1 1
eye(n) buyrug’i birlik matritsani hosil qiladi.
Misol.
>> 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 0 1
Endi shu matritsani m-faylga funksiyasini yaratamiz (3.10-rasm). 3.10-rasm.
Ushbu m-faylga birlik matritsa hosil qiladigan protsedura yasadik va uning nomini
diagonal.m deb nomladik. Endi bu m-fayl yordamida diagonal(n) buyrug’i hosil bo’ldi.
Endi ushbu buyruq yordamida ham eye(n) buyrug’ining bajargan ishini bajarsa bo’ladi:
>> diagonal(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 0 1
tril (A) - berilgan matritsaning diagonaildan yuqori qismini elementlarini 0 bilan almashtirishni
amalga oshiradi.
Misol. Berilgan matritsaning diagonaildan yuqori qismini elementlarini 0 bilan almashtirish:
Dostları ilə paylaş: |