Mohammad Saad
Mohammad Saad

Reputation: 2005

How to make an array from for loop globally available in Julia?

I am writing a code in Julia. Here, I have a dataframe of size 272x32. My objective is to find the minimum in each column, and store them in an array of 32 elements. To execute this I have created a for loop iterating over the range of columns in dataframe. However, upon assigning the values to be global the output stored has only the last value. But, I would like it to have minimum of column as each entry in array which I can use it for matrix arithmetic operations.

The code snippet:

n = ncol(variables)

for i in 1:n

    global mins = minimum(variables[!, i])
    global maxs = maximum(variables[!, i])

end

Please advice to resolve this issue.

Regards,

Upvotes: 5

Views: 188

Answers (2)

Mohammad Saad
Mohammad Saad

Reputation: 2005

There is one more approach to solve this issue,

mins = zeros(n)
maxs = zeros(n)
n = ncol(variables)

for i in 1:n

    mins[i] = minimum(variables[!, i])
    maxs[i] = maximum(variables[!, i])

end

But I don't know how it will affect the execution speed or memory allocation?

Upvotes: 0

Bogumił Kamiński
Bogumił Kamiński

Reputation: 69839

I am not 100% sure if this is what you are asking for but it seems it is:

mins = minimum.(eachcol(variables))
maxs = maximum.(eachcol(variables))

Upvotes: 3

Related Questions