---
title: "Lab1 Solution"
output: pdf_document
---

# Vectors
### 1.	Create a vector u that has values -10, -9, -8, ...,0.  How many different ways can you use?
```{r}
u <- c(-10, -9, -8, -7, -6, -5, -4, -3, -2, -1, 0)
u <- -10:0
u <- seq(-10,0)
# In the following two lines, from = and to = can be omitted.
u <- seq(from = -10, to = 0, by = 1)
u <- seq(from = -10, to = 0, length.out = 11)
```
###2.	Create another vector v that has values -0.1, 0.4, 0.9, 1.4, ., and there are 11 numbers (aka terms) in v. How many different ways can you use?
```{r}
v <- c(-0.1, 0.4, 0.9, 1.4, 1.9, 2.4, 2.9, 3.4, 3.9, 4.4, 4.9)
# In the following line, from = can be omitted.
v <- seq(from = -0.1, by = 0.5, length.out = 11)
print(v)
	```
	
### 3.	Calculate the vector of u+v and u*v.
```{r}
u+v
u*v
```
### 4.	Increase all terms in u by 1, and then take away 20% from all terms in v.
```{r}
u+1
(1-0.2)*v
```
###5.	Create a vector w that contains all the numbers from u and then v. Report the length of w. 
```{r}
w <- c(u,v)
print(w)
length(w)
```
###6.	Use a command to return the 14th, 15th and 16th value of w. What about the 2nd, the 5th, 9th and 21st value of w? What is the 23rd value?
```{r}
w[14:16]
w[c(2,5,9,21)]
w[23]    # No error is reported, but a NA
```
###7.	Replace the 3rd term of w by 100. Then replace the 7th, 15th and 22nd terms by 200, 300 and 400 simultaneously. 
```{r}
w[3] <- 100
w[c(7,15,22)] <- c(200,300,400)
print(w)
```
###8.	Remove u.
```{r}
rm(u)
```
###9.	Remove all the objects in the environment. 
```{r}
rm(list = ls())
```

###10. Create a vector ```p``` of the values of $e^xcos(x)$ at $x = 3, 3.1, 3.2, . . . , 6$.

```{r}
x <- seq(3,6,by=0.1)
p <- exp(x)*cos(x)
p
```


### 11. Find the maximum/minimum value in ```p``` and the index (position) of that value in ```p```. 

```{r}
max_in_p <- max(p)
min_in_p <- min(p)
index_max <- which.max(p)  #first index
index_min <- which.min(p)  #first index 
indices_max <- which(p == max(p)) #all indices of the max value
indices_min <- which(p == min(p)) #all indices of the max value
print(max_in_p) 
print(min_in_p)
print(index_max)
print(index_min)
```

### 12. Sort ```p``` in the descending order.
```{r}
sort(p, decreasing = T)
```

### 13. Create (4, 6, 3, 4, 6, 3, . . . , 4, 6, 3) where there are 10 occurrences of 4.
```{r}
tmp <- c(4,6,3)
rep(tmp,10)
```

### 14. Create (4, 4, . . . , 4, 6, 6, . . . , 6, 3, 3, . . . , 3) where there are 10 occurrences of 4, 20 occurrences of 6 and 30
occurrences of 3.
```{r}
rep(tmp,times = c(10,20,30))
```

#Matrices
###1. Create the following matrix and assign it to the variable `b_matrix`.

``` {r}
b_matrix <- matrix(
  seq(1,39,by=2),4,5,byrow=T,
  dimnames = list(
    c("A", "B", "C", "D"),
    c("a", "b", "c", "d", "e")
  ))
b_matrix
```
###2. Extract a sub-matrix from `b_matrix` named `subB` as follows. Try to use as many possible ways as you can (positive and negative indices). 

```{r}
subB <- b_matrix[c(1,2,4),c(2,3)]
subB <- b_matrix[-c(3),-c(1,4,5)]
subB <- b_matrix[c(1,2,4),-c(1,4,5)]
subB <- b_matrix[-c(3),c(2,3)]
# all the above will result in the same subB
subB
```

###3. In R, `%*%` is an operator for matrix multiplication. Compute `a_matrix %*% b_matrix` and `a_matrix %*% subB`. Discuss the results you get from R. 
```{r}
a_matrix <- matrix(
  1:12,
  nrow = 4,            #ncol = 3 works the same
  dimnames = list(
    c("one", "two", "three", "four"),
    c("eins", "zwei", "drei")
  )
)

dim(a_matrix)
dim(b_matrix)
dim(subB)

# a_matrix %*% b_matrix 
# The above command doesn't work and throws an non-comformable arguments error.
# As the number of columns in a_matrix isn't equal to the number of rows in b_matrix. 
# Specifically, ncol(a_matrix) != nrow(b_matrix)
a_matrix %*% subB

```

###4. Create three vectors  ```x,y,z```  with integers and each vector has 3 elements. Combine the three vectors to become a $3\times 3$ matrix  `A`  where each column represents a vector. Change the row names to  `a,b,c`.
```{r}
x <- c(1,2,3)
y <- c(4,5,6)
z <- c(7,8,9)
A <- cbind(x,y,z) 

rownames(A) <- c("a","b","c")
colnames(A) <- c("X","Y","Z")  #add column namess
print(A)

#if combined by rows:
B <- rbind(x,y,z)
print(B)
```

