Having a difficult time wrapping my head around Anonymous function and Comprehensions coding, not an easy thing for someone who thinks in ecological terms.
What I am doing is doing a life-table calculation for the proportion of the cohort that has survived at time t. Basically my code looks like this.
for idx = 1:length (VarData)
buf - = VarData [idx]
push!(cof,(buf/GrandTotal))
end
where
buf is the variable represents the number of individuals surviving at each time point and comes from taking the current survivorship and subtracting the number who died.
VarData is a vector array that contains the number of individuals that died in that time period.
GrandTotal is the total number of individuals present at time point zero.
So if initially
buf=60
GrandTotal=60
and VarData=[1 1 6 7 16 19 8 2 0]
this results in the following vector array
[0.983 0.966 0.866 0.75 0.483 0.166 0.033]
which is the survivors (buf) divided by GrandTotal
The code above gives the correct answer but I canât help but think there is some form of anonymous function or comprehension that I can use to make it more Julia (if that is the right word) that I cannot see.
It will give you directly the answer you are looking for.
Please note that there is a period before the minus (-) and before the slash (/). These are necessay in order to add (or divide into) a scalar and a vector.
vector / scalar is allowed without dot, because vector * inv(scalar) is a standard operation on vector spaces, just like vector + vector.
However, if you use vector ./ scalar and vector .+ vector, it computes the same thing but has the advantage of âfusingâ with other dot calls. e.g. (scalar .- vector) ./ scalar) is computed in a single loop and allocates a single array. See also the âMore Dotsâ blog post.
(Nor is there is anything wrong with writing a loop âmanuallyâ in Julia.)
For both @vsoler and my solution, if you keep using a row vector as data (numbers separated by spaces instead of commas) you need to add dims=2 to the cumsum function as keyword argument.
Note: that the data is imported from a CSV and it is arranged properly in the code. I provided the data within VarData to illustrate in the best way possible what I was trying to accomplish. I apologize for the wrong syntax but it does seem that everyone did understand the goal here.