# How to calculate Euclidean distance between a tuple and each tuple in a Vector using map in Julia?

I want to calculate the Euclidean distance between a tuple and each tuple within a Vector in Julia using the map function, like below (but I get two values instead of three): julia> tups = [ (1, 3), (11, 2), (0, 1) ]; julia> map((x, y) -> √(sum((x.-y).^2)), tups, (3, 3)) 2-element Vector{Float64}: 2.0 8.06225774829855… Read More How to calculate Euclidean distance between a tuple and each tuple in a Vector using map in Julia?

# Efficient way to copy a matrix except for one column

Consider a matrix where you don’t need the third column: X = zeros(Int64, (8, 3)); X[:, 1] = [0, 0, 0, 0, 1, 1, 1, 1]; X[:, 2] = [1, 1, 2, 2, 1, 1, 2, 2]; julia> X 8×3 Matrix{Int64}: 0 1 0 0 1 0 0 2 0 0 2 0 1 1… Read More Efficient way to copy a matrix except for one column

# Create a matrix with repetitive values from a slice of another matrix

Suppose I have this matrix: julia> mat=[1 2 3;4 5 6] 2×3 Matrix{Int64}: 1 2 3 4 5 6 Now I want to achieve something like this using a standard function: 4×3 Matrix{Int64}: 1 2 3 1 2 3 1 2 3 1 2 3 Note that the important thing to me is using the… Read More Create a matrix with repetitive values from a slice of another matrix

# How to store results from loop in Julia

Suppose that I have a function that makes some calculation and return a dataframe in Julia using DataFrames, Random function dummy_df(;theta) A = randn(100); B = randn(100); A = A .* theta; B = B .* theta df_ = DataFrame(A = A, B = B) return(df_) end a = dummy_df(theta = 1) Now suppose I… Read More How to store results from loop in Julia

# Modify the last dimension of a multi-dimensional array of arbitrary dimensions

Suppose I have a two dimensional array. test_array = ones(2,2) I can modify the last dimension of test_array by doing test_array[:,1] = -99. If I had test_array = ones(2,2,2), I can do test_array[:,:,1] = – 99. Suppose I want to write a function where I want to modify an array of arbitrary dimensions. For example,… Read More Modify the last dimension of a multi-dimensional array of arbitrary dimensions

# Replace values with for loop

Suppose I have the following function: function y1(x) y = x^(2) – 4 return y end Now, I want to evaluate all the values from this sequence: collect(range(-10,10, 1000)) I tried this y_1 = zeros(1000); for x in collect(range(-10, 10, 1000)) y_1 = y1.(x) end Note that I use the broadcast operator to apply the… Read More Replace values with for loop

# How to get each index of one or more of an array's dimensions?

If I want eachindex but only of a specific dimension, what’s a good way to accomplish this? E.g. x is a 3x5x7 Array x = rand(3,5,7) And I’d like to get the 2nd dimension’s indexes of 1:5, ideally in a way that doesn’t assume that the indexing starts at 1 >Solution : The axes function… Read More How to get each index of one or more of an array's dimensions?

# Julia – Passing two arguments to make array, iterate through first argument but not second

I am new to Julia and would be very appreciative of your help. I have a function that takes an integer and an array of integers, which will return an integer. I want to make an array of the results of this function over a range. However, I am struggling to get Julia to recognise… Read More Julia – Passing two arguments to make array, iterate through first argument but not second

# How to use `endswith()` against multiple values in Julia?

I have a string and I want to use endswith() on it but against multiple values. My first guess was to try it with a tuple: # {string} suffixes = ({multiple suffixes here}) endswith(i,extensions) This generated the error message: MethodError: no method matching endswith(::String, ::Tuple{String, String}) So, I went looking for official documentation at Julia… Read More How to use `endswith()` against multiple values in Julia?

# Check if a record exists in a Julia array

Say we have a matrix: A = [1.0 2.0 3.0; 4.0 5.0 6.0] #2×3 Matrix{Float64} and a record: b = [1.0 2.0 3.0] #1×3 Matrix{Float64} what is the most efficient way to check if record b exists in matrix A in Julia? Doing b in A returns false. And writing a nested for-loop when we… Read More Check if a record exists in a Julia array