I have a function that maps a vector to a tuple. I would like to apply this to a matrix by rows, and get a tuple of vectors (mapslices gives me a vector of tuples, I tried zip but I could not get it working). MWE (inelegant, using the first element):
function maprows(f, A)
B = vec(mapslices(f, A, 1))
ntuple(i->map(x->x[i], B), length(B[1]))
end
f(x) = (x[1:2], x[3], x[4:5]) # number of elements is not necessarily 3
A = ones(10, 5)
C = maprows(f, A)
What is your question? Does your maprows function what you expect?
A way to convert the output of mapslices to a tuple of vectors would be for example:
julia> maprows(f, A) = tuple(collect.(mapslices(f, A, 1))...)
maprows (generic function with 1 method)
julia> C = maprows(f, A)
(Any[[1.0, 1.0], 1.0, [1.0, 1.0]],
Any[[1.0, 1.0], 1.0, [1.0, 1.0]],
Any[[1.0, 1.0], 1.0, [1.0, 1.0]],
Any[[1.0, 1.0], 1.0, [1.0, 1.0]],
Any[[1.0, 1.0], 1.0, [1.0, 1.0]])