Suppose I want to create a vector in Julia. The vector should be [0,0.5,1,1.5,2,....20].
What's the quick command to create a vector like this, from 0 to 20, increasing by 0.5?
This will create a range, which is a kind of vector:
0:0.5:20
This does the same thing
range(0, 20; step=0.5)
If you absolutely need to make a Vector, you can use collect on either of these, but in most cases you should not, and just use 0:0.5:20 directly.
There are many ways to do this. While I prefer the colon array syntax already mentioned, this could be used for more general construction:
[0.5*i for i in 1:20]
Related
I have the following function: problema_firma_emprestimo(r,w,r_emprestimo,posicao,posicao_banco), where all input are scalars.
This function return three different matrix, using
return demanda_k_emprestimo,demanda_l_emprestimo,lucro_emprestimo
I need to run this function for a series of values of posicao_banco that are stored in a vector.
I'm doing this using a for loop, because I need three separate matrix with each of them storing one of the three outputs of the function, and the first dimension of each matrix corresponds to the index of posicao_banco. My code for this part is:
demanda_k_emprestimo = zeros(num_bancos,na,ny);
demanda_l_emprestimo = similar(demanda_k_emprestimo);
lucro_emprestimo = similar(demanda_k_emprestimo);
for i in eachindex(posicao_bancos)
demanda_k_emprestimo[i,:,:] , demanda_l_emprestimo[i,:,:] , lucro_emprestimo[i,:,:] = problema_firma_emprestimo(r,w,r_emprestimo[i],posicao,posicao_bancos[i]);
end
Is there a fast and clean way of doing this using vectorized functions? Something like problema_firma_emprestimo.(r,w,r_emprestimo[i],posicao,posicao_bancos) ? When I do this, I got a tuple with the result, but I can't find a good way of unpacking the answer.
Thanks!
Unfortunately, it's not easy to use broadcasting here, since then you will end up with output that is an array of tuples, instead of a tuple of arrays. I think a loop is a very good approach, and has no performance penalty compared to broadcasting.
I would suggest, however, that you organize your output array dimensions differently, so that i indexes into the last dimension instead of the first:
for i in eachindex(posicao_bancos)
demanda_k_emprestimo[:, :, i] , ...
end
This is because Julia arrays are column major, and this way the output values are filled into the output arrays in the most efficient way. You could also consider making the output arrays into vectors of matrices, instead of 3D arrays.
On a side note: since you are (or should be) creating an MWE for the sake of the people answering, it would be better if you used shorter and less confusing variable names. In particular for people who don't understand Portuguese (I'm guessing), your variable names are super long, confusing and make the code visually dense. Telling the difference between demanda_k_emprestimo and demanda_l_emprestimo at a glance is hard. The meaning of the variables are not important either, so it's better to just call them A and B or X and Y, and the functions foo or something.
Usually when I want to define the last unknown unit number when calling a series I would use;
z <- length(data)
mean(data[3:z])
However isn't there a much simpler way to define the last unit in the same statement without having to call and define length as a separate variable? Like a special symbol to imply the last unit.
mean(data[3:length(data)]) should work if you don't want an exra variable..?
There isn't really a shortcut for that but instead of selecting from 3 to length, you can also remove first 2 elements which can be done using indexing :
data[-(1:2)]
Or using tail
tail(data, -2)
We can use
data[tail(names(data), -2)]
I have to solve a problem with permutations. The function takes vector a with n elements as a parameter. I declare b as #variable - there should be the permutation 1:n that gives the best result after finding the solution of a problem.
The error appears when I want to create #constraint. I have to use a[b[1]], so it takes the first element from vector which is a variable. It gives my error, that I can't use type VariableRef as a index of an array. But how can I get around this when I have to use it?
I sounds as if you have two optimisation problems one of which is an integer programming problem. You might think about separating the two.
(Sorry for not writing a comment, my reputation is still too low ;-) )
I am trying to find an efficient way to create a new array by repeating each element of an old array a different, specified number of times. I have come up with something that works, using array comprehensions, but it is not very efficient, either in memory or in computation:
LENGTH = 1e6
A = collect(1:LENGTH) ## arbitrary values that will be repeated specified numbers of times
NumRepeats = [rand(20:100) for idx = 1:LENGTH] ## arbitrary numbers of times to repeat each value in A
B = vcat([ [A[idx] for n = 1:NumRepeats[idx]] for idx = 1:length(A) ]...)
Ideally, what I would like would be a structure akin to the sparse matrix apparatus that Julia has but that would instead store data efficiently based on the indices where repeated values occur. Barring that, I would at least like an efficient way to create a vector such as B in the example above. I looked into the repeat() function, but as far as I can tell from the documentation and my experimentation with the function, it is just for repeating slices of an array the same number of times for each slice. What is the best way to approach this?
Sounds like you're looking for run-length encoding. There's an RLEVectors.jl package here: https://github.com/phaverty/RLEVectors.jl. Not sure how usable it is. You could also make your own data type fairly easily.
Thanks for trying RLEVectors.jl. Some features and optimizations had been languishing on master without a version bump. It can definitely be mixed with other vectors for element-wise arithmetic. I'll put the linear algebra operations on the feature request list. Any additional feature suggestions would be most welcome.
RLEVectors.jl has a rep function that works like R's and RLEVectors.inverse_ree is like StatsBase.inverse_rle, but it works on run ends rather than lengths.
Suppose a vector of integers like this:
vect<-c(2,3,4)
i want to change this vector to another vector in which all elements are multiplied by 2 and subtracted from 20 .and get result like this:
result<-c(16,14,12)
in Python, there is a simple method like this:
result=[20-(item*2) for item in vect]
i know it can be easily done with a loop but i was wondering if there is any similar short method as in python in R? its probably a very basic question but im new to R and could not find the answer anywhere else.
Simply
vect2 <- 20 - vect * 2