ماتریس ها (Matrices)
«ماتریس (Matrix)» جدول دو بعدی است. سطر دارد و ستون دارد. برای اعداد یا متن هم کار می کند. مثل جدول نمره های کلاس.
ساخت ماتریس با matrix()
با تابع matrix() بساز. با nrow تعداد سطر و با ncol تعداد ستون را بده.
نمونه: ماتریس عددی 3×2
# Create a matrix
thismatrix <- matrix(c(1, 2, 3, 4, 5, 6), nrow = 3, ncol = 2)
# Print the matrix
thismatrix
نکته: تابع c() آیتم ها را پشت سرهم می چسباند.
نمونه: ماتریس رشته ای 2×2
# Create a character matrix
thismatrix <- matrix(c("apple", "banana", "cherry", "orange"), nrow = 2, ncol = 2)
# Print the matrix
thismatrix
دسترسی به خانه ها
با براکت [] دسترسی بگیر. اول سطر، بعد ستون نوشته می شود.
نمونه: یک خانه خاص
thismatrix <- matrix(c("apple", "banana", "cherry", "orange"), nrow = 2, ncol = 2)
# Row 1, Column 2
thismatrix[1, 2]
نمونه: یک سطر کامل
thismatrix <- matrix(c("apple", "banana", "cherry", "orange"), nrow = 2, ncol = 2)
# Whole row 2
thismatrix[2, ]
نمونه: یک ستون کامل
thismatrix <- matrix(c("apple", "banana", "cherry", "orange"), nrow = 2, ncol = 2)
# Whole column 2
thismatrix[, 2]
چند سطر یا چند ستون
با c() چند ایندکس بده. خروجی زیرماتریس می شود.
نمونه: چند سطر
thismatrix <- matrix(c("apple", "banana", "cherry", "orange", "grape", "pineapple", "pear", "melon", "fig"), nrow = 3, ncol = 3)
# Rows 1 and 2
thismatrix[c(1, 2), ]
نمونه: چند ستون
thismatrix <- matrix(c("apple", "banana", "cherry", "orange", "grape", "pineapple", "pear", "melon", "fig"), nrow = 3, ncol = 3)
# Columns 1 and 2
thismatrix[, c(1, 2)]
افزودن ستون و سطر
با cbind() ستون اضافه کن و با rbind() سطر اضافه کن.
نمونه: افزودن ستون
thismatrix <- matrix(c("apple", "banana", "cherry", "orange", "grape", "pineapple", "pear", "melon", "fig"), nrow = 3, ncol = 3)
# Add a column
newmatrix <- cbind(thismatrix, c("strawberry", "blueberry", "raspberry"))
# Print result
newmatrix
نکته: طول ستون افزوده باید با سطرها برابر باشد.
نمونه: افزودن سطر
thismatrix <- matrix(c("apple", "banana", "cherry", "orange", "grape", "pineapple", "pear", "melon", "fig"), nrow = 3, ncol = 3)
# Add a row
newmatrix <- rbind(thismatrix, c("strawberry", "blueberry", "raspberry"))
# Print result
newmatrix
نکته: طول سطر افزوده باید با ستون ها برابر باشد.
حذف سطر و ستون
با اندیس منفی در c() حذف کن. سطر و ستون را جداگانه بده.
نمونه: حذف سطر 1 و ستون 1
thismatrix <- matrix(c("apple", "banana", "cherry", "orange", "mango", "pineapple"), nrow = 3, ncol = 2)
# Remove first row and first column
thismatrix <- thismatrix[-c(1), -c(1)]
# Print result
thismatrix
جستجوی آیتم و ابعاد
برای وجود آیتم از %in% استفاده کن. برای ابعاد از dim() کمک بگیر.
نمونه: آیا "apple" وجود دارد؟
thismatrix <- matrix(c("apple", "banana", "cherry", "orange"), nrow = 2, ncol = 2)
# Membership test
"apple" %in% thismatrix
نمونه: تعداد سطر و ستون
thismatrix <- matrix(c("apple", "banana", "cherry", "orange"), nrow = 2, ncol = 2)
# Rows and columns
dim(thismatrix)
نکته: تعداد کل خانه ها برابر سطر ضرب در ستون است.
طول ماتریس
تابع length() تعداد کل خانه ها را می دهد. برای 2×2 برابر 4 است.
نمونه: length ماتریس
thismatrix <- matrix(c("apple", "banana", "cherry", "orange"), nrow = 2, ncol = 2)
# Total cells
length(thismatrix)
حلقه روی ماتریس
با دو حلقه for بچرخ. اول سطرها، سپس ستون ها را برو.
نمونه: چاپ تک تک خانه ها
thismatrix <- matrix(c("apple", "banana", "cherry", "orange"), nrow = 2, ncol = 2)
# Nested loops over matrix
for (rows in 1:nrow(thismatrix)) {
for (columns in 1:ncol(thismatrix)) {
print(thismatrix[rows, columns])
}
}
ترکیب دو ماتریس
با rbind() سطری و با cbind() ستونی ترکیب کن.
نمونه: ترکیب سطری و ستونی
# Two 2x2 matrices
Matrix1 <- matrix(c("apple", "banana", "cherry", "grape"), nrow = 2, ncol = 2)
Matrix2 <- matrix(c("orange", "mango", "pineapple", "watermelon"), nrow = 2, ncol = 2)
# Combine by rows
Matrix_Combined <- rbind(Matrix1, Matrix2)
Matrix_Combined
# Combine by columns
Matrix_Combined <- cbind(Matrix1, Matrix2)
Matrix_Combined
گام های تمرینی
- یک ماتریس 3×3 بساز و چاپ کن.
- سطر دوم و ستون اول را بگیر.
- یک ستون جدید با
cbind()اضافه کن. - خانه خاصی را جستجو و نتیجه را ببین.
- با دو حلقه همه خانه ها را چاپ کن.
جمع بندی سریع
- ماتریس جدول دو بعدیِ سطر×ستون است.
matrix()می سازد؛nrowوncolاندازه می دهند.- دسترسی با
[row, col]انجام می شود. cbindوrbindبرای افزودن و ترکیب هستند.dimوlengthابعاد و کل خانه ها را می دهند.