rep(0,10)
## [1] 0 0 0 0 0 0 0 0 0 0
1:49
## [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
## [24] 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
## [47] 47 48 49
v<- c(1,10,20,30)
rev(v)
## [1] 30 20 10 1
v<- 0:11
#matrix(v,4,3) or
matrix(data=v, nrow = 4, ncol=3)
## [,1] [,2] [,3]
## [1,] 0 4 8
## [2,] 1 5 9
## [3,] 2 6 10
## [4,] 3 7 11
# in python numpy, the matrix is filled row-wise first unlike in R where column is filled first.
# we can make the same by transposing
t(matrix(v,3,4))
## [,1] [,2] [,3]
## [1,] 0 1 2
## [2,] 3 4 5
## [3,] 6 7 8
## [4,] 9 10 11
v<-c(1,2,0,0,4,0)
which(!v==0)
## [1] 1 2 5
diag(3)
## [,1] [,2] [,3]
## [1,] 1 0 0
## [2,] 0 1 0
## [3,] 0 0 1
array(runif(9),dim=c(3,3,3)) # create six random numbers as vector, then arrange as matrix
## , , 1
##
## [,1] [,2] [,3]
## [1,] 0.00939768 0.3088938 0.2595010
## [2,] 0.53535069 0.3616628 0.6331503
## [3,] 0.65492216 0.6957565 0.7657420
##
## , , 2
##
## [,1] [,2] [,3]
## [1,] 0.00939768 0.3088938 0.2595010
## [2,] 0.53535069 0.3616628 0.6331503
## [3,] 0.65492216 0.6957565 0.7657420
##
## , , 3
##
## [,1] [,2] [,3]
## [1,] 0.00939768 0.3088938 0.2595010
## [2,] 0.53535069 0.3616628 0.6331503
## [3,] 0.65492216 0.6957565 0.7657420
a<-matrix(runif(100),10,10)
max(a);min(a)
## [1] 0.9821908
## [1] 0.004889003
mean(runif(30))
## [1] 0.5209409
z<-matrix(rep(1,100),10,10)
z[2:9,2:9] <- 0
z
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
## [1,] 1 1 1 1 1 1 1 1 1 1
## [2,] 1 0 0 0 0 0 0 0 0 1
## [3,] 1 0 0 0 0 0 0 0 0 1
## [4,] 1 0 0 0 0 0 0 0 0 1
## [5,] 1 0 0 0 0 0 0 0 0 1
## [6,] 1 0 0 0 0 0 0 0 0 1
## [7,] 1 0 0 0 0 0 0 0 0 1
## [8,] 1 0 0 0 0 0 0 0 0 1
## [9,] 1 0 0 0 0 0 0 0 0 1
## [10,] 1 1 1 1 1 1 1 1 1 1
file:"C:\Users\mahes\100 exercises in R.Rmd"