ALGORITME & PEMROGRAMAN Abdul Kudus, SSi., MSi., PhD. Senin, 6.30 – 9.00 Rabu, 10.00 – 12.00 Rabu, 12.00 – 14.00
Matriks dan Array Membuat Matriks Salah satu cara membuat matriks adalah dengan perintah matrix(). > y <- matrix(c(1,2,3,4),nrow=2,ncol=2) > y [,1] [,2] [1,] 1 3 [2,] 2 4 > y[,2] [1] 3 4 diisi kolom per kolom Cara lain membuatnya adalah menyatakan unsur secara sendiri-sendiri > y <- matrix(nrow=2,ncol=2) > y[1,1] <- 1 > y[2,1] <- 2 > y[1,2] <- 3 > y[2,2] <- 4 > y [,1] [,2] [1,] 1 3 [2,] 2 4
Kita bisa mengisi baris per baris > m <- matrix(c(1,2,3,4,5,6),nrow=2,byrow=T) > m [,1] [,2] [,3] [1,] 1 2 3 [2,] 4 5 6 Operasi Matriks - Operasi Aljabar Lineir > y %*% y # perkalian matriks [,1] [,2] [1,] 7 15 [2,]10 22
> 3*y # perkalian dengan skalar [,1] [,2] [1,] 3 9 [2,] 6 12 > y+y # tambah [1,] 2 6 [2,] 4 8 - Pengindeksan (Subskrip) Matriks Ambil kolom ke-2 dan ke-3 > z [,1] [,2] [,3] [1,] 1 1 1 [2,] 2 1 0 [3,] 3 0 1 [4,] 4 0 0 > z[,2:3] [,1] [,2] [1,] 1 1 [2,] 1 0 [3,] 0 1 [4,] 0 0
Memberi nilai kepada submatriks > y [,1] [,2] [1,] 1 4 [2,] 2 5 [1,]11 12 [2,]21 22 [3,]31 32 > y[2:3,] [,1] [,2] [1,]21 22 [2,]31 32 > y[2:3,2] [1] 22 32 Memberi nilai kepada submatriks > y [,1] [,2] [1,] 1 4 [2,] 2 5 [3,] 3 6 > y[c(1,3),] <- matrix(c(1,1,8,12),nrow=2) [1,] 1 8 [3,] 1 12
> x <- matrix(nrow=3,ncol=3) > y <- matrix(c(4,5,2,3),nrow=2) > y [,1] [,2] [1,] 4 2 [2,] 5 3 > x[2:3,2:3] <- y > x [,1] [,2] [,3] [1,] NA NA NA [2,] NA 4 2 [3,] NA 5 3 > y [,1] [,2] [1,] 1 4 [2,] 2 5 [3,] 3 6 > y[-2,] [2,] 3 6 buang baris ke-2
Contoh: Mengedit Gambar > library(pixmap) > logo <- read.pnm(system.file("pictures/logo.pgm", package="pixmap")[1]) > logo Pixmap image Type : pixmapGrey Size : 77x101 Resolution : 1x1 Bounding box : 0 0 101 77
> plot(logo) intensitas bernilai antara 0.0 (hitam) sampai 1.0 (putih). > str(logo) Formal class 'pixmapGrey' [package "pixmap"] with 6 slots ..@ grey : num [1:77, 1:101] 1 1 0.996 0.996 1 ... ..@ channels: chr "grey" ..@ size : int [1:2] 77 101 ..@ cellres : num [1:2] 1 1 ..@ bbox : num [1:4] 0 0 101 77 ..@ bbcent : logi FALSE
Misal pixel pada baris 35, kolom 55 abu-abu > logo@grey[35,55] [1] 0.1960784 posisi gambar hurup R logo2 <- logo logo2@grey[20:77,40:95] <- 0 #dihitamkan plot(logo2)
Membuat bagian huruf R di-blur bagian.blur <- function(gbr,rows,cols,q) { lrows <- length(rows) lcols <- length(cols) gbr.baru <- gbr blur <- matrix(nrow=lrows, ncol=lcols,runif(lrows*lcols)) gbr.baru @grey[rows,cols] <- (1-q)*gbr@grey[rows,cols] + q*blur return(gbr.baru) } logo3 <- bagian.blur(logo,20:77,40:95,0.65) plot(logo3)
- Menyaring (Filtering) dalam Matriks Prosesnya > x <- matrix(c(1:3,2:4),ncol=2) > x x [1,] 1 2 [2,] 2 3 [3,] 3 4 > x[x[,2] >= 3,] [1,] 2 3 [2,] 3 4 > j <- x[,2] >= 3 > j [1] FALSE TRUE TRUE Lalu gunakan utk menyaring > x[j,] x [1,] 2 3 [2,] 3 4 Atau bisa langsung > x[x[,2] >= 3,] x [1,] 2 3 [2,] 3 4
> M <- matrix(c(5,2,9,-1,10,11),ncol=2) [,1] [,2] [1,] 5 -1 [2,] 2 10 [3,] 9 11 > which(M > 2) [1] 1 3 5 6 > z <- c(5,12,13) > x[z %% 2 == 1,] [,1] [,2] [1,] 1 2 [2,] 3 4 Contoh lain > m <- matrix(1:6,ncol=2) > m [,1] [,2] [1,] 1 4 [2,] 2 5 [3,] 3 6 > m[m[,1] > 1 & m[,2] > 5,] [1] 3 6 Mengapa begini?