# A column vector array1 = [1, 2, 3] # The type of the object array1 typeof(array1) # A row vector array2 = [1 2 3] # The transpose transpose(array1) # With a mix of types, all the elements inherent the "highest" type array2 = [1, 2, 3.0] # Index for one of the original integers will be Float64 array2[1] # Column-wise entry of multidimensional array array3 = [[1, 2, 3] [4, 5, 6] [7, 8, 9]] # Row-wise entry of multidimensional array array4 = [[1 2 3]; [4 5 6]; [7 8 9]] # Length of array3 length(array3) length(array4) # Index order of column-wise array for i in 1:length(array3) println("Element $(i) is ", array3[i]) end # Index order of row-wise array for i in 1:length(array4) println("Element $(i) is ", array4[i]) end # Using repeat() to repeat column elements repeat([1, 2], 3) # Using repeat() to repeat row elements repeat([1 2], 3) # Using range(start, step, number of elements) range(1, step = 1, length = 10) # Create collections using the collect() function collect(range(1, step = 1, length = 10)) # Short-hand syntax collect(1:10) # Creating empty array with two rows and three columns array5 = Array{Union{Missing, Int}}(missing, 2, 3) # Reshaping reshape(array5, 3, 2) # Creating a 10 x 5 array with each element drawn randomly from value 10 through 20 array6 = rand(10:20, 10, 5) #All rows in first column array6[:, 1] # Rows two through five of second column array6[2:5, 2] # Values in rows 2, 4, 6, and in columns 1 and 5 array6[[2, 4, 6], [1, 5]] # Values in row 1 from column 3 to the last column array6[1, 3:end] # Boolean logic (returning only true or false) array6[:, 1] .> 12 # Creating a five element array array7 = [1, 2, 3, 4, 5] # Permanantly append 10 to end of array push!(array7, 10) pop!(array7) # Change second element value to 1000 array7[2] = 1000 # Viewing the change array7 # An example of list comprehension array8 = [3 * i for i in 1:5] # Column-wise collection iterating through second element first array9 = [a * b for a in 1:3, b in 1:3] # Elementwise addition of a scalar using dot notation array9 .+ 1 # Elementwise addition of similar sized arrays array7 + array8 # Propagation missing + 1 missing > 1 [1, 2, 3, missing, 5] + [10, 20, 30, 40 ,50] # Checking equality of value using == # Cannot return true or false since value is not known missing == missing # Checking equality of type with === missing === missing # Checking type equality with isequal() isequal(missing, missing) # Sorting with isless() isless(1, missing) # Checking on infinity isless(Inf, missing) # A 3 x 3 array of integer zeros array11 = zeros(Int8, 3, 3) # A 3 x 3 array of floating point ones array12 = ones(Float16, 3, 3) # Array of true (bit array) values array13 = trues(3, 3) # Fill an array with elements of value x array14 = fill(10, 3, 3) # Convert elements to a different data type convert.(Float16, array14) # Concatenate arrays along rows (makes rows) array15 = [1, 2, 3] array16 = [10, 20, 30] cat(array15, array16, dims = 1) # Same as above vcat(array15, array16) # Concatenate arrays along columns (makes columns) cat(array15, array16, dims = 2) # Same as above hcat(array15, array16) # Tuples with mixed types tuple1 = (1, 2., π, 4, "Julia") # For loop to look at value and type of each element for i in 1:length(tuple1) println(" The value of the tuple at index number $(i) is $(tuple1[i]) and the type is $(typeof(tuple1[i])).") end # Each element can be named a, b, c, seven = (1, 3, 5, 7) a seven # Reverse order index (can be done with arrays too) tuple1[end:-1:1] # Mixed length tuples tuple2 = ((1, 2, 3), 1, 2, (3, 100, 1)) # Element 4 tuple2[4] # Element 2 in element 4 tuple2[4][2] # 1 Example of a dictionary dictionary1 = Dict(1 => 77, 2 => 66, 3 => 1) # The => is shorthand for the Pair() function dictionary2 = Dict(Pair(1,100), Pair(2,200), Pair(3,300)) # 2 Specifying types dictionary3 = Dict{Any, Any}(1 => 77, 2 => 66, 3 => "three") # We can get a bit crazy dictionary4 = Dict{Any, Any}("a" => 1, (2, 3) => "hello") # Using symbols as keys dictionary5 = Dict(:A => 300, :B => 305, :C => 309) dictionary5[:A] # Using in() to check on key-value pairs in((:A => 300), dictionary5) # Changing an existing value dictionary5[:C] = 1000 dictionary5 # Using the delete!() function delete!(dictionary5, :A) # The keys of a dictionary keys(dictionary5) values(dictionary5) # Creating a dictionary with automatic keys procedure_vals = ["Appendectomy", "Colectomy", "Cholecystectomy"] procedure_dict = Dict{AbstractString,AbstractString}() # An empty dictionary with types specified for (s, n) in enumerate(procedure_vals) procedure_dict["x_$(s)"] = n end procedure_dict # Iterating through a dictionary by key and value for (k, v) in procedure_dict println(k, " is ",v) end # Sorting dictionary6 = Dict("a"=> 1,"b"=>2 ,"c"=>3 ,"d"=>4 ,"e"=>5 ,"f"=>6) # Sorting using a for loop for k in sort(collect(keys(dictionary6))) println("$(k) is $(dictionary6[k])") end