If I have a vector:
x = [1,2,6,3]
And I want to calculate the ratio of the numbers, what's an easy way to do this?
E.g. I want:
[2.0,3.0,0.5]
CodePudding user response:
You can either write a loop,
[x[i 1] / x[i] for i = 1:length(x)-1]
or use a vectorized division:
@views x[2:end] ./ x[1:end-1]
3-element Vector{Float64}:
2.0
3.0
0.5
In terms of performance, both would be identical. The vectorized form, however, might be the preferred approach for more generic (think GPU) and arguably more readable code.