I wish to create a function which can accept one input and perform calculations, or can accept multiple inputs, choose the best one, and then perform calculations. I’m not sure how to approach it. I originally thought that I would keep the “options” in the last dimension of an AbstractArray
. A toy example is below, but I cannot get sort
to work. It gives a method error when I pass a Vector
or a Range
. I next thought about writing one method for AbstractVector
s and another for AbstractArray
s. However, I do not think that approach would extend to higher dimensions, because I do not know how I would call different methods on a 2D vs 3D array. Finally, I thought about creating a vector of arrays where the outer vector construct would contain the “options”, but that may make the code more complex than it needs to be. Is there a preferred way to handle this situation? (Coding critiques are also welcome.)
function f(x)
il=size(x,1)
jl=size(x,2)
#sort!(x,dims=1) # this works for f(b), but breaks for f(a) and f(c).
y=Array{typeof(x[1])}(undef,il)
for i in 1:il
y[i]=sum(x[i,:])
end
(minsum,jminsum)=findmin(sum(x,dims=1))
z=x[:,jminsum]
return y,z
end
a=[4,2,3,1]
b=[[5,8,6,7] [11,10,9,12]]
c=10:-2:0
f(a)
f(b)
f(c)