I am new to Julia and looking for a function to compute the proportions of multidimensional array give some dimensions as margin. Basically, it is dividing each element of the array by the sum of element in the desired dimensions. In R, proportions()
is a function to do this.
Asked
Active
Viewed 51 times
1

Mohammad
- 97
- 5
1 Answers
2
After some search and thanks to @DNF, I came up with this function:
function proportions(x; d = 1)
dim = (setdiff(1:ndims(x), d)...,)
return x ./ sum(x, dims = dim)
end
and a similar function for the equivalent R function marginSums()
:
function marginSums(x; d = 1)
dim = (setdiff(1:ndims(x), d)...,)
return dropdims(sum(x, dims = dim), dims = dim)
end

Mohammad
- 97
- 5
-
2Simplification: use `filter(!in(d), 1:ndims(x))` or just `setdiff(1:ndims(x), d)` – DNF Jul 11 '23 at 07:55