# 2.1 x <- c(1,2,3);y <- c(4,5,6) e <- c(1,1,1) #1) z <- 2*x+y+e;z #2) crossprod(x,y) #3) outer(x,y) #2.2 #按列输入(默认) A <- matrix(1:20,c(4,5)) #按行输入 B <- matrix(1:20,c(4,5),byrow = TRUE) #1) C <- A+B;C #2) D <- A*B;D #3) #4) A[1:3,1:3] #5) G <- B[,-3] #2.3 x <- c(rep(1,5),rep(3,4),rep(4,2));x #2.4 #1) Hilbert <- function(n){ H <- matrix(,n,n) for(i in 1:n){ for(j in 1:n){ H[i,j] = 1/(i+j-1) } } return(H) } #1) Hilbert(5) #2) solve(Hilbert(5)) #3) eigen(Hilbert(5)) #2.5 #2.7 fun <- function(n){ if (n <= 0) { print("Insert positive number !");break} while (n != 1){ if(n %% 2 == 0){n = n/2} else{n = 3*n+1} if (n == 1) print("success!") } } # v2 fun_1 <- function(n){ if (n <= 0) { print("Insert positive number !");break} while (n != 1){ if(n %% 2 == 0){n = n/2} else{n = 3*n+1} } print("success!") } try(fun(-1),silent = T) fun(13) fun_1(13)
时间: 2024-10-25 23:18:47