Matriks tidak lain adalah susunan persegi panjang dari data atau angka. Dengan kata lain, itu adalah array persegi panjang dari data atau angka. Entri horizontal dalam matriks disebut sebagai 'baris' sedangkan entri vertikal disebut sebagai 'kolom'. Jika suatu matriks memiliki r banyaknya baris dan c banyaknya kolom maka ordo matriks tersebut adalah r x c. Setiap entri dalam matriks dapat berupa bilangan bulat, atau nilai mengambang, atau bahkan bilangan kompleks
Contoh
// 3 x 4 matrix 1 2 3 4 M = 4 5 6 7 6 7 8 9 // 2 x 3 matrix in Python A = ( [ 2, 5, 7 ], [ 4, 7, 9 ] ) // 3 x 4 matrix in Python where entries are floating numbers B = ( [ 1.0, 3.5, 5.4, 7.9 ], [ 9.0, 2.5, 4.2, 3.6 ], [ 1.5, 3.2, 1.6, 6.5 ] )Di Python, kita dapat mengambil matriks masukan pengguna dengan berbagai cara. Beberapa metode untuk matriks input pengguna dengan Python ditunjukkan di bawah ini
Kode #1
# A basic code for matrix input from user
R= int(input(Enter the number of rows:2 Enter the number of columns:3 Enter the entries rowwise: 1 2 3 4 5 6 1 2 3 4 5 6 1Enter the number of rows:2 Enter the number of columns:3 Enter the entries rowwise: 1 2 3 4 5 6 1 2 3 4 5 6 2
Enter the number of rows:2 Enter the number of columns:3 Enter the entries rowwise: 1 2 3 4 5 6 1 2 3 4 5 6 3= int(input(Enter the number of rows:2 Enter the number of columns:3 Enter the entries rowwise: 1 2 3 4 5 6 1 2 3 4 5 6 9Enter the number of rows:2 Enter the number of columns:3 Enter the entries rowwise: 1 2 3 4 5 6 1 2 3 4 5 6 2
Enter the number of rows:2 Enter the number of columns:2 Enter the entries in a single line separated by space: 1 2 3 1 [[1 2] [3 1]]_2
Enter the number of rows:2 Enter the number of columns:2 Enter the entries in a single line separated by space: 1 2 3 1 [[1 2] [3 1]]3= Enter the number of rows:2 Enter the number of columns:2 Enter the entries in a single line separated by space: 1 2 3 1 [[1 2] [3 1]]5
Enter the number of rows:2 Enter the number of columns:2 Enter the entries in a single line separated by space: 1 2 3 1 [[1 2] [3 1]]6(Enter the number of rows:2 Enter the number of columns:2 Enter the entries in a single line separated by space: 1 2 3 1 [[1 2] [3 1]]8Enter the number of rows:2 Enter the number of columns:2 Enter the entries in a single line separated by space: 1 2 3 1 [[1 2] [3 1]]9
# A basic code for matrix input from user_1
# A basic code for matrix input from user2 # A basic code for matrix input from user3# A basic code for matrix input from user4 # A basic code for matrix input from user5# A basic code for matrix input from user6# A basic code for matrix input from user7
# A basic code for matrix input from user8# A basic code for matrix input from user9=Enter the number of rows:2 Enter the number of columns:2 Enter the entries in a single line separated by space: 1 2 3 1 [[1 2] [3 1]]5
________33______8# A basic code for matrix input from user2 4# A basic code for matrix input from user4 # A basic code for matrix input from user5 7 8
9R0int(inputR4
# A basic code for matrix input from user8R6
R_8
# A basic code for matrix input from user2 # A basic code for matrix input from user3# A basic code for matrix input from user4 # A basic code for matrix input from user5=3
________33______8# A basic code for matrix input from user2 4# A basic code for matrix input from user4 # A basic code for matrix input from user5=9
________37______0Enter the number of rows:2 Enter the number of columns:2 Enter the entries in a single line separated by space: 1 2 3 1 [[1 2] [3 1]]6int2= int4Enter the number of rows:2 Enter the number of columns:2 Enter the entries in a single line separated by space: 1 2 3 1 [[1 2] [3 1]]9
# A basic code for matrix input from user8Enter the number of rows:2 Enter the number of columns:2 Enter the entries in a single line separated by space: 1 2 3 1 [[1 2] [3 1]]6int8
Keluaran
Satu baris
int_9
(0= (2int(input(6________333______________________________________________________________________.
Kode #2. Menggunakan fungsi input7 dan input8
Di Python, ada perpustakaan populer bernama NumPy. Perpustakaan ini adalah perpustakaan dasar untuk perhitungan ilmiah apa pun. Ini juga digunakan untuk array multidimensi dan seperti yang kita ketahui matriks adalah array persegi panjang, kita akan menggunakan pustaka ini untuk matriks input pengguna
input9 Enter the number of rows:2 Enter the number of columns:3 Enter the entries rowwise: 1 2 3 4 5 6 1 2 3 4 5 6 00
R= int(input(Enter the number of rows:2 Enter the number of columns:3 Enter the entries rowwise: 1 2 3 4 5 6 1 2 3 4 5 6 1Enter the number of rows:2 Enter the number of columns:3 Enter the entries rowwise: 1 2 3 4 5 6 1 2 3 4 5 6 2
Enter the number of rows:2 Enter the number of columns:3 Enter the entries rowwise: 1 2 3 4 5 6 1 2 3 4 5 6 3= int(input(Enter the number of rows:2 Enter the number of columns:3 Enter the entries rowwise: 1 2 3 4 5 6 1 2 3 4 5 6 9Enter the number of rows:2 Enter the number of columns:3 Enter the entries rowwise: 1 2 3 4 5 6 1 2 3 4 5 6 2
Enter the number of rows:2 Enter the number of columns:2 Enter the entries in a single line separated by space: 1 2 3 1 [[1 2] [3 1]]6(Enter the number of rows:2 Enter the number of columns:3 Enter the entries rowwise: 1 2 3 4 5 6 1 2 3 4 5 6 22Enter the number of rows:2 Enter the number of columns:2 Enter the entries in a single line separated by space: 1 2 3 1 [[1 2] [3 1]]9
Enter the number of rows:2 Enter the number of columns:3 Enter the entries rowwise: 1 2 3 4 5 6 1 2 3 4 5 6 _25
Enter the number of rows:2 Enter the number of columns:3 Enter the entries rowwise: 1 2 3 4 5 6 1 2 3 4 5 6 _26
Enter the number of rows:2 Enter the number of columns:3 Enter the entries rowwise: 1 2 3 4 5 6 1 2 3 4 5 6 27= Enter the number of rows:2 Enter the number of columns:3 Enter the entries rowwise: 1 2 3 4 5 6 1 2 3 4 5 6 29(Enter the number of rows:2 Enter the number of columns:3 Enter the entries rowwise: 1 2 3 4 5 6 1 2 3 4 5 6 31(intEnter the number of rows:2 Enter the number of columns:3 Enter the entries rowwise: 1 2 3 4 5 6 1 2 3 4 5 6 34inputEnter the number of rows:2 Enter the number of columns:3 Enter the entries rowwise: 1 2 3 4 5 6 1 2 3 4 5 6 36