-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathExamples1.R
More file actions
114 lines (86 loc) · 2.81 KB
/
Examples1.R
File metadata and controls
114 lines (86 loc) · 2.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
################################################################################
#Example 1 - simplest case
#add 1 to each element in a vector
################################################################################
#create vector 1 to 10
x0 <- 1:10
#for loop version
x1 <- x0
for (i in 1:length(x0)){
x1[i] <- x0[i]+1
}
#vectorized version
x2 <- x0+1
identical(x1,x2)
################################################################################
#Example 2 - sequential standard operations
#multiply with 3 and then add 2 to each element in a vector
################################################################################
#create vector 1 to 10
x0 <- 1:10
#for loop version
x1 <- x0
for (i in 1:length(x0)){
x1[i] <- (x0[i]*3)+2
}
#vectorized version
x2 <- (x0*3)+2
identical(x1,x2)
################################################################################
#Example 3 - if cases
#multiply with 3 if the element is larger than 5 for each element in a vector
################################################################################
#create vector 1 to 10
x0 <- 1:10
#for loop version
x1 <- x0
for (i in 1:length(x0)){
if(x0[i] > 5) x1[i] <- (x0[i]*3)
}
#vectorized version
x2 <- x0
x2[x0>5] <- (x0[x0>5]*3)
identical(x1,x2)
#Note: it can be achieved in one line if we replace the elements in x0 instead
# creating the extra vector x2.
################################################################################
#Example 4 - nested for loop
# Add 2 to each element
################################################################################
#create 10x10 matrix
x0 <- matrix(1:100, ncol=10)
#for loop version
x1 <- x0
for (i in 1:nrow(x0)){
for (j in 1:ncol(x0)){
x1[i,j] <- x0[i,j]+2
}
}
#vectorized version
x2 <- x0+2
identical(x1,x2)
################################################################################
#Example 5 - multiple manipulations
# Add 2 to each element if lower than 5
# Add 5 to each element if higher than 10
################################################################################
#create vector 1 to 10
x0 <- 1:10
#for loop version
x1 <- x0
for (i in 1:length(x0)){
if(x0[i] < 5) x1[i] <- (x0[i]+2)
if(x0[i] > 10) x1[i] <- (x0[i]+5)
}
#vectorized version
x2 <- x0
x2[x0<5] <- (x0[x0<5]+2)
x2[x0>10] <- (x0[x0>10]+5)
identical(x1,x2)
#Note: We had to pay the price of having two lines of code to cover the two
# if statements, but observe there is no need to use more variables.
# The price we pay for vectorization is primary memory. But usually
# it is easy to design the code so if it becomes expensive in respect
# to RAM, then we can split up in pieces that fit into the RAM and run
# them sequentially like a for loop, or even better to distribute to
# different nodes on a clustes for a simulatneous evaluation.