Sie sind auf Seite 1von 15

Exercises 1:

Write matrix a in an R script, run the current line (ctrl+enter or cmd+enter), and call it from
the R console.

1. Script jawaban:
a <- matrix(data = c(1,2,3,4),
nrow=2, ncol=2, byrow=TRUE)

2. Hasil eksekusi :

Exercises 2:

What is the output of a[-1,] and a[,-2]?

= Output dari a[-1, ]= 3 dan 4 dan a[, -2] = 1 dan 3. Maksud dari a[-1, ] itu untuk menghilangkan
baris 1. Sedangkan maksud dari a[, -2] itu untuk menghilangkan kolom 2.

1. Script jawaban :
a <- matrix(c(1,2,3,4),
2, 2, TRUE)

a[1, 2]
a[-1, ]
a[, -2]
a[1:2, ]
a[, 1:2]
diag(a)
2. Hasil eksekusi :
Exercises 3:

What a[a > 3] <- 2 does?

= Maksud dari script a[a > 3] <- 2 adalah setiap angka yang pada matriks a jika nilainya lebih
dari 3 maka nilai pada matriks tersebut diganti menjadi 2.

1. Script jawaban :

a <- matrix (c(1,2,3,4) ,2, 2, TRUE)

a[a>3] <- 2

2. Hasil eksekusi :

Exercises 4:

Why a[-1,] %*% b[-1,] doesn’t work? What the error says?

= Karena jumlah pada baris matriks a[-1,] itu berbeda dengan jumlah kolom matriks b[-1,]
sehingga tidak dapat dilakukan perkalian matriks. Akan tetapi, pada program ini perkaliannya
dapat dilakukan.
Exercises 5:

Why solve(b) won’t work? What the error says?

= Karena nilai determinan dari matriks b mendekati 0, yaitu 6.661338e-16

Exercises 6:

1. Check ?sample
2. Sample 100 numbers in the range of [1,1000], without replacement,
and assign those to a variable named values.
3. Create a 10 ⇥ 10 matrix G, with elements from values, filling by row.
4. Do the following tasks.
1. Create a 10 ⇥ 10 matrix H, which is a transpose of G.
2. Define a matrix J, which is an addition of G and H.
3. Compute the determinant of G, H, and J.
4. Create a matrix K, which is a combination of the first 5 columns of G
and J

1.
2. Script jawaban:
values <- sample(x=1:1000, size=100, replace=FALSE)
Hasil eksekusi:
3. Script jawaban:
values <- sample(x=1:1000, size=100, replace=FALSE)
G <- matrix (c(values),10,10,TRUE)
Hasil eksekusi:

4. Do the following tasks.


1. Script jawaban:
values <- sample(x=1:1000, size=100, replace=FALSE)
G <- matrix (c(values),10,10,TRUE)
H <- t(G)
Hasil Eksekusi:
2. Script jawaban:
values <- sample(x=1:1000, size=100, replace=FALSE)
G <- matrix (c(values),10,10,TRUE)
H <- t(G)
J <- G + H
Hasil Eksekusi:
3. Script jawaban:
values <- sample(x=1:1000, size=100, replace=FALSE)
G <- matrix (c(values),10,10,TRUE)
H <- t(G)
J <- G + H
det(G)
det(H)
det(J)
Hasil Eksekusi:
4. Script jawaban:
values <- sample(x=1:1000, size=100, replace=FALSE)
G <- matrix (c(values),10,10,TRUE)
H <- t(G)
J <- G + H
det(G)
det(H)
det(J)
K <- cbind(G[,5], J[,5])
Hasil Eksekusi:

Exercises 7:

Write an if-then condition that says

1. if b is a square matrix, then (matrix) multiply b by the transpose of b


2. if the number of rows of b is smaller than that of its columns, Then add b with a
multiplication of b by 5.
3. if the number of rows of b is greater than that of its columns, Then subtract a multiplication
of b by 6, from b, i.e., b-b⇥6.

=
1. Script jawaban:
b <- matrix(c(1:9),3,3,TRUE)
if (ncol(b) == nrow(b)){
b= b%*%t(b)
}

Hasil Eksekusi:

2. Script jawaban:
- Jika nrow lebih besar dari ncol:
b <- matrix(c(1:12),3,4,TRUE)
if (ncol(b) > nrow(b)){
b= b*5
}

Hasil Eksekusi:
3. Script jawaban:
- Jika nrow lebih besar dari ncol:
b <- matrix(c(1:12),4,3,TRUE)
if (ncol(b) < nrow(b)){
b= b*6
}else{
b= b-b*6
}
- Jika nrow lebih kecil daripada ncol:
b <- matrix(c(1:12),3,4,TRUE)
if (ncol(b) < nrow(b)){
b= b*6
}else{
b= b-b*6
}

Hasil Eksekusi:

- Jika nrow lebih besar dari ncol:


- Jika nrow lebih kecil daripada ncol:

Exercises 8:

Write R script with if-then and for looping as follows.

1. if b is a square matrix, then use for-loop to print b1,j , where j Refers to all columns of b.
2. if the number of rows of b is smaller than that of its columns, Then use nested for-loop to
multiply bi,j by 5, where i and j refer to The number of rows and columns of b, respectively.
3. if the number of rows of b is greater than that of its columns, Ude nested for-loop to
subtract bi,j x 6 from bi,j , where i and j refer do the number of rows and columns of b,
respectively.

1. Jawaban script:
b <- matrix(c(1:9),3,3,TRUE)
if (ncol(b) == nrow(b)){
for (i in 1:ncol(b)) {
print(b[1,i])
}
}
Hasil eksekusi:
2. Jawaban script:
b <- matrix(c(1:12),3,4,TRUE)
if (nrow(b)< ncol(b)){
for (i in 1:nrow(b)) {
for(j in 1:ncol(b)){
b[i,j] <- b[i,j]*5
print(b[1,j])
}
}
}

Hasil Eksekusi:

3. Jawaban script:
b <- matrix(c(1:12),4,3,TRUE)
if (nrow(b)> ncol(b)){
for (i in 1:nrow(b)) {
for(j in 1:ncol(b)){
b[i,j] <- b[i,j]-b[i,j]*6
print(b[1,j])
}
}
}
Hasil Eksekusi:

Das könnte Ihnen auch gefallen