Upload presentasi
Presentasi sedang didownload. Silahkan tunggu
1
ALGORITME & PEMROGRAMAN
Abdul Kudus, SSi., MSi., PhD. Senin, 6.30 – 9.00 Rabu, – 12.00 Rabu, – 14.00
2
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
3
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
4
> 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
5
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
6
> 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
7
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 :
8
> 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] channels: chr "grey" size : int [1:2] cellres : num [1:2] 1 1 bbox : num [1:4] bbcent : logi FALSE
9
Misal pixel pada baris 35, kolom 55 abu-abu
> [1] posisi gambar hurup R logo2 <- logo <- 0 #dihitamkan plot(logo2)
10
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)) <- + q*blur return(gbr.baru) } logo3 <- bagian.blur(logo,20:77,40:95,0.65) plot(logo3)
12
- 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
13
> M <- matrix(c(5,2,9,-1,10,11),ncol=2)
[,1] [,2] [1,] [2,] [3,] > which(M > 2) [1] > z <- c(5,12,13) > x[z %% 2 == 1,] [,1] [,2] [1,] [2,] Contoh lain > m <- matrix(1:6,ncol=2) > m [,1] [,2] [1,] [2,] [3,] > m[m[,1] > 1 & m[,2] > 5,] [1] 3 6 Mengapa begini?
Presentasi serupa
© 2024 SlidePlayer.info Inc.
All rights reserved.