julia has very useful broadcasting for vector
f (generic function with 1 method)
julia> f.(1:3,6) # or @. f(1:3,6)
julia> A=hcat(f.(1:3,6), f.(1:3,7), f.(1:3,8))
6 7 8
9 10 11
14 15 16
Is there any macro or function can do like
julia> A == @??? f(1:3,6:8)
note: the number of parameters of f is no limit.
You could try:
map(t -> f(t...), Iterators.product(1:3, 6:8))
Aren’t you just looking for
. is missing …!!
This is a good answer but seems just satisfy 2-dims array…
f.(x, y, z, ...)) works for any number of dimensions, and will expand into ℝⁿ (or other n-dimensional spaces), but you need to get your inputs in the right shape first. So if you want
z to lie in the third dimension, you can do
z = reshape(1:3, 1, 1, :)
If you need to do this for arbitrarily high dimensions, it becomes tedious, and @greg_plowman’s solutions is more convenient. If your data is already in the right shape, then it’s better to just use
f.(x, y, z, ...).
This answer is useful for me. Thanks!
Obviously, it does not want to be an alternative to the solution with product, but maybe could be useful in other cases to have a generalization (perhaps made better) of this type:
r(p,v)=reshape(v, ntuple(e-> e < p ? 1 : length(v), p))
another way to define the position based reshaping function …