443-970-2353
[email protected]
CV Resume
# Row vector: size (1, n)
z = [1 2 3] # or [1; 2; 3]' or [1, 2, 3]'
z = t(c(1, 2, 3))
z
dim(z)
# Column vector: size (n, 1)
z = [1 2 3]'
z = cbind(c(1, 2, 3))
z
dim(z)
# Column vector: size (n, )
# 1d array
z = [1; 2; 3] # or [1, 2, 3]
z = array(c(1, 2, 3))
z
dim(z)
# Integers from A to C with step size B
z = collect(1:2:10) # A = 1, B = 2, C = 10
# collect() builds an array from range
z = seq(from = 1, to = 10, by = 2)
z
# To go down, use a negative step value
collect(10:-1:2)
z = seq(from = 10,to = 2, by = -1)
z
# Linearly spaced vector of n points
z = linspace(2, 20, 4) # n = 4
z = seq(from = 2, to = 20, length.out = 4)
z
# Create a matrix
z = [1 2; 3 4]
z
z = matrix(c(1, 2, 3, 4), ncol = 2, byrow = TRUE)
z
# 3 x 3 matrix of zeros
z = zeros(3,3)
z = matrix(0, 3, 3)
z
# 3 x 3 matrix of ones
z = ones(3,3)
z = matrix(1, 3, 3)
z
# 3 x 3 identity matrix
z = eye(3) # or eye(3,3)
z = diag(3)
z
z = falses(3, 4)
z = matrix(FALSE, 3,4)
z
z = trues(3, 4)
z = matrix(TRUE, 3,4)
z
# Diagonal matrix
z = diagm([3, 4, 5]) # or diagm([3; 4; 5])
z = diag(c(3,4,5))
z
# Uniform random numbers
z = rand(5,2)
z = matrix(runif(10), ncol = 2)
z
# Normal random numbers
z = randn(5,2)
z = matrix(rnorm(10), ncol = 2)
z
# Transpose
z = [1,2,3]
z.' # or transpose(z)
z = matrix(1:12, ncol = 3) # R is column-major
t(z)
# Concatenate horizontally
z1 = [1, 2, 3]
z2 = [4, 5, 6]
[z1 z2] # or hcat(z1,z2)
z1 = c(1, 2, 3)
z2 = c(4, 5, 6)
cbind(z1, z2)
# Concatenate vertically
z1 = [1 2 3]
z2 = [4 5 6]
[z1, z2] # or vcat(z1,z2)
z1 = c(1, 2, 3)
z2 = c(4, 5, 6)
rbind(z1, z2)
# Reshape
z1 = 1:20
z2 = reshape(z1, 4,5)
z1 = 1:20
z2 = matrix(z1, ncol = 5)
z2
# Convert matrix to vector
z1 = [1 2 3; 4 5 6]
z = z1[:] # or vec(z1)
# Julia is column-major like R
z1 = matrix(c(1, 2, 3, 4, 5, 6), ncol = 3, byrow = TRUE)
z1
as.vector(z1)
# Flip left/right
z = reshape(1:9,3,3)
flipdim(z, 2)
z = matrix(1:9, ncol = 3)
z
z[ ,ncol(z):1]
# Flip up/down
z = reshape(1:9,3,3)
flipdim(z, 1)
z = matrix(1:9, ncol = 3)
z
z[nrow(z):1, ]
# Access one element: Julia uses one-based indexing
z = reshape(range(1,20), 4,5)
z[1,1]
z = matrix(1:20, ncol = 5)
z[1,1] # R uses one-based indexing
z = fill("Julia", 3,3)
z = matrix(rep("R", 9), ncol = 3)
z
# Access specific rows
zc = z[1:2,:] # rows 1 and 2; all columns
z = matrix(1:20, ncol = 5)
z[1:2, ]
# Access specific columns
zr = z[:,3:4] # columns 3 and 4; all rows
z[, 3:4]
z = reshape(1:12, 3,4)
z[end - 1:end, end - 1: end]
z = matrix(1:12, ncol = 4)
z[(nrow(z)-1):nrow(z), (ncol(z)-1):ncol(z)]
# Get dimensions of an array
z = reshape(1:24, 3,4,2)
size(z)
z = array(1:24, c(3, 4, 2))
dim(z)
# Diagonals of a matrix
z = reshape(1:20, 4,5)
diag(z)
z = matrix(1:20, nrow = 4)
diag(z)
# Dot product
z1 = 1:10
z2 = 1:10
dot(z1,z2) # or z1 ⋅ z2
z1 = 1:10
z2 = 1:10
z1 %*% z2
# Matrix multiplication
z1 = reshape(1:9,3,3)
z2 = reshape(1:6, 3,2)
z1 * z2
z1 = matrix(1:9, ncol = 3)
z2 = matrix(1:6, ncol = 2)
z1 %*% z2
# Element-wise multiplication
z1 = reshape(1:9,3,3)
z2 = reshape(1:9, 3,3)
z1 .* z2
z1 = matrix(1:9, ncol = 3)
z2 = matrix(1:9, nrow = 3)
z1*z2
# Matrix to a power
z = reshape(range(1,16), 4,4)
z^2
z = matrix(1:16, ncol = 4)
z %*% z
# Matrix to a power, elementwise
z.^2
z^2
# Inverse of a matrix
z = rand(2:10, 3, 3)
inv(z)
z = matrix(sample(2:10, 9), ncol = 3)
solve(z)
# Determinant
z = rand(2:10, 3, 3)
det(z)
det(z)
# Eigenvalues and eigenvectors
val, vec = eig(z.^2);
println("val:")
println(val)
println("vec:")
println(vec)
eigen(z)
# comparison
A = [1 2 3 4]
B = [3 2 3 6]
A == B
A = c(1, 2, 3, 4)
B = c(3, 2, 3, 6)
identical(A, B)
# Elementwise comparison
A = [1 2 3 4]
B = [3 2 3 6]
A .== B
A = c(1, 2, 3, 4)
B = c(3, 2, 3, 6)
A == B
z = [3 4 5; 6 7 9; 15 -20 25]
maximum(z) # of entire array
z = matrix(c(3, 4, 5, 6, 7, 9, 15, -20, 25), ncol = 3, byrow = TRUE)
max(z)
z = [3 4 5; 6 7 9; 15 -20 25]
minimum(z) # of entire array
z = matrix(c(3, 4, 5, 6, 7, 9, 15, -20, 25), ncol = 3, byrow = TRUE)
min(z)
z = [3 4 5; 6 7 9; 15 -20 25]
sum(z) # of entire array
z = matrix(c(3, 4, 5, 6, 7, 9, 15, -20, 25), ncol = 3, byrow = TRUE)
sum(z)
z = [3 4 5; 6 7 9; 15 -20 25]
maximum(z, 1) # of each column
z = matrix(c(3, 4, 5, 6, 7, 9, 15, -20, 25), ncol = 3, byrow = TRUE)
apply(z, 2, max)
z = [3 4 5; 6 7 9; 15 -20 25]
maximum(z, 2) # of each row
z = matrix(c(3, 4, 5, 6, 7, 9, 15, -20, 25), ncol = 3, byrow = TRUE)
apply(z, 1, max)
z = [3 4 5; 6 7 9; 15 -20 25]
minimum(z, 1) # of each column
z = matrix(c(3, 4, 5, 6, 7, 9, 15, -20, 25), ncol = 3, byrow = TRUE)
apply(z, 2, min)
z = [3 4 5; 6 7 9; 15 -20 25]
minimum(z, 2) # of each row
z = matrix(c(3, 4, 5, 6, 7, 9, 15, -20, 25), ncol = 3, byrow = TRUE)
apply(z, 1, min)
z = [3 4 5; 6 7 9; 15 -20 25]
sum(z, 1) # of each column
z = matrix(c(3, 4, 5, 6, 7, 9, 15, -20, 25), ncol = 3, byrow = TRUE)
colSums(z) # or apply(z, 2, sum)
z = [3 4 5; 6 7 9; 15 -20 25]
sum(z, 2) # of each row
z = matrix(c(3, 4, 5, 6, 7, 9, 15, -20, 25), ncol = 3, byrow = TRUE)
rowSums(z) # or apply(z, 1, sum)
z = [3 4 5; 6 7 9; 15 -20 25]
cumsum(z, 1) # Cumulative sum by column
z = matrix(c(3, 4, 5, 6, 7, 9, 15, -20, 25), ncol = 3, byrow = TRUE)
apply(z, 2, cumsum)
z = [3 4 5; 6 7 9; 15 -20 25]
cumsum(z, 2) # Cumulative sum by row
z = matrix(c(3, 4, 5, 6, 7, 9, 15, -20, 25), ncol = 3, byrow = TRUE)
t(apply(z, 1, cumsum))
# Compare two (or more) arrays element by element, returning a new array with the largest values from each:
z1 = [3 4 6 90 12 -40]
z2 = [5 7 10 -20 40 -10]
max(z1,z2)
z1 = c(3, 4, 6, 90, 12, -40)
z2 = c(5, 7, 10, -20, 40, -10)
pmax(z1,z2)
# Compare two (or more) arrays element by element, returning a new array with the largest values from each:
z1 = [3 4 6 90 12 -40]
z2 = [5 7 10 -20 40 -10]
min(z1,z2)
z1 = c(3, 4, 6, 90, 12, -40)
z2 = c(5, 7, 10, -20, 40, -10)
pmin(z1,z2)
# Setting the contents of arrays
a = rand(0:10, 10, 10);
a[a .== 0] = -100
a
a = matrix(sample(0:10, 100,replace = TRUE),ncol = 10)
a[a == 0] =-100
a
# remove element/s
a = collect(1:10)
splice!(a,1:5)
a
a = 1:10
a[-(1:5)]
# concatenate
a = collect(1:10)
push!(a, 20) # at the end
# or [a;20]
a = 1:10
c(a, 20)
# concatenate
a = collect(1:10)
unshift!(a, 20) # at the beginning
# or [20; a]
a = 1:10
c(20,a)
# check if a value is in an array
z = 1:10
9 in z
z = 1:10
9 %in% z
# insert to specific locations
a = [1, 3, 4, 5]
splice!(a, 2, 2:3)
a
a = c(1, 3, 4, 5)
c(a[1], 2:3, a[3:length(a)])
# remove last element
z = collect(1:10)
pop!(z)
z
z = 1:10
z[-length(z)]
# remove first element
z = collect(1:10)
shift!(z)
z
z = 1:10
z[-1]
# delete by indices
z = collect(1:10)
deleteat!(z, [1 4 6])
z = 1:10
z[-c(1,4,6)]
y = [ 2 3 5; -6 3 8; -9 3 11]
find(isodd, y) # gives indices of odd numbers
y = matrix(c(2, 3, 5, -6, 3, 8, -9, 3, 11),
ncol = 3, byrow = TRUE)
which(y%%2 !=0)
# Returns the row and column count of the array
x = randn(12,15,16);
size(x)
x = array(rnorm(12*15*16),c(12, 15, 16))
dim(x)
# How many elements the array contains
x = randn(12,15,16);
length(x)
x = array(rnorm(12*15*16),c(12, 15, 16))
length(x)
# How many non-zero
x = [1 2 0 ; 4 5 0]
countnz(x)
x = matrix(c(1, 2, 0, 4, 5, 0), ncol =3, byrow = TRUE)
length(x[x !=0])
union(1:6,4:10) # removes duplicates
union(1:6, 4:10)
# Intersection of two or more arrays
intersect(1:10,7:15)
intersect(1:10, 7:15)
# Elements that are in the first array but not the second
setdiff(1:10,7:15)
setdiff(1:10,7:15)
# Elements that are in the first array but not the second
setdiff(7:15,1:10)
setdiff(7:15, 1:10)
y = [ 2 3 5; -6 3 8; -9 3 20]
filter(iseven,y) # gives the evenones
y = matrix(c(2, 3, 5, -6, 3, 8,-9, 3, 20
),ncol = 3, byrow = TRUE)
y[y%%2 ==0]
# Count the number of elements that satisfy the condition
count(isodd, 1:5000)
y = 1:5000
length(y[y%%2 != 0])
# check if any of the elements satisfy the condition
any([1 2 3 10 12] .== [3 4 5 10 25])
any(c(1, 2, 3, 10, 12)==c(3, 4, 5, 10, 25))
# check if all the elements satisfy the condition
all([1 2 3 10 12] .== [3 4 5 10 25])
all(c(1, 2, 3, 10, 12)==c(3, 4, 5, 10, 25))
# Random element from an array
z = range(1, 3, 20)
z[rand(1:end)]
z = seq(from = 1, by = 3, length.out = 20)
sample(z, 1)
# Find the extreme values of an array:
z = 1:2:200
extrema(z)
z = seq(from = 1, to = 200, by =2)
range(z)
z = [1 2 3; 4 5 6; 7 8 9]
extrema(z, 1)
z = matrix(c(1, 2, 3, 4, 5, 6, 7, 8, 9),
ncol = 3, byrow = TRUE)
apply(z, 2, range)
z = [1 2 3; 4 5 6; 7 8 9]
extrema(z, 2)
z = matrix(c(1, 2, 3, 4, 5, 6, 7, 8, 9),
ncol = 3, byrow = TRUE)
t(apply(z, 1, range))
# find product
z = [1 2 3; 4 5 6; 7 8 9]
prod(z)
z = matrix(c(1, 2, 3, 4, 5, 6, 7, 8, 9),
ncol= 3, byrow = TRUE)
prod(z)
# find product
z = [1 2 3; 4 5 6; 7 8 9]
prod(z, 1) # for each column
z = matrix(c(1, 2, 3, 4, 5, 6, 7, 8, 9),
ncol= 3, byrow = TRUE)
apply(z, 2, prod)
# find product
z = [1 2 3; 4 5 6; 7 8 9]
prod(z, 2) # for each row
z = matrix(c(1, 2, 3, 4, 5, 6, 7, 8, 9),
ncol= 3, byrow = TRUE)
apply(z, 1, prod)
# find mean
z = [1 2 3; 4 5 6; 7 8 9]
mean(z)
z = matrix(c(1, 2, 3, 4, 5, 6, 7, 8, 9),
ncol= 3, byrow = TRUE)
mean(z)
# find mean
z = [1 2 3; 4 5 6; 7 8 9]
mean(z, 1) # for each column
z = matrix(c(1, 2, 3, 4, 5, 6, 7, 8, 9),
ncol= 3, byrow = TRUE)
apply(z, 2, mean)
# find mean
z = [1 2 3; 4 5 6; 7 8 9]
mean(z, 2) # for each row
z = matrix(c(1, 2, 3, 4, 5, 6, 7, 8, 9),
ncol= 3, byrow = TRUE)
apply(z, 1, mean)
# find median
z = [1 2 3; 4 5 6; 7 8 9]
middle(z)
z = matrix(c(1, 2, 3, 4, 5, 6, 7, 8, 9),
ncol= 3, byrow = TRUE)
median(z)
# find median
z = [1 2 3; 4 5 6; 7 8 9]
mean(z, 1) # for each column
z = matrix(c(1, 2, 3, 4, 5, 6, 7, 8, 9),
ncol= 3, byrow = TRUE)
apply(z, 2, median)
# for loop
z = Int64[]
for i = 1:10
z = [z; i^2];
end
z
z = c()
for(i in 1:10){
z = c(z, i^2)
}
z
# if condition
evens = Int64[]
for i = 1:20
if i%2 ==0
evens = [evens; i]
end
end
evens
evens = c()
for(i in 1:20){
if(i%%2 ==0){
evens = c(evens, i)
}
}
evens
# while loop and if/else
i = 1
while i <= 20
if i%2 ==0
println(i," is even")
else
println(i," is odd")
end
i = i+1
end
i = 1
while(i <= 20){
if(i%%2 == 0){
cat(paste(i, "is even\n"))
}else{
cat(paste(i, "is odd\n"))
}
i = i + 1
}
# sinle line function
f(x) = 9/5*x + 32
f(-40)
f <- function(x) 9/5*x + 32
f(-40)
# Multiple line function
function f(x)
if x%2 ==0
return("Yor number is even")
else
return(" Yor number is odd")
end
end
f(-40)
f <- function(x){
if(x%%2 ==0){
cat(" Your number is even")
}else{
cat("your number is odd")
}
}
f(-40)
a = [1 2 3;-4 -5 6; 7 2 -9]
sort(a, 1) # sort along columns
# we can also use sortcolumns()
a = matrix(c(1, 2, 3, -4, -5, 6, 7, 2, -9),
ncol = 3, byrow = TRUE)
apply(a, 2, sort)
a = [1 2 3;-4 -5 6; 7 2 -9]
sort(a, 2) # sort along rows
# we can also use sortrows()
a = matrix(c(1, 2, 3, -4, -5, 6, 7, 2, -9),
ncol = 3, byrow = TRUE)
t(apply(a, 1, sort))
a = [1 2 3;-4 -5 6; 7 2 -9]
sort(a, 1, rev = true) # decreasing order
a = matrix(c(1, 2, 3, -4, -5, 6, 7, 2, -9),
ncol = 3, byrow = TRUE)
apply(a, 2, sort, decreasing = TRUE)