Sapply (from R) Julia's equivalent?

Suppose I have a 2-dimensional array, and I want to apply several functions to each of its columns. Ideally, I would like to return the results as a matrix (with one row for each function and one column for each input column).

The following code generates the values ​​I want, but as an array of arrays.

A = rand(10,10) [mapslices(f, A, 1) for f in [mean median iqr]] 

Another similar example is here [ Julia: using pmap with matrices

Is there a better syntax for returning results as a 2-dimensional array instead of an array of arrays?

What I really like is something like the sapply function from R. [ https://stat.ethz.ch/R-manual/R-devel/library/base/html/lapply.html]

+7
julia-lang
source share
2 answers

You can use an anonymous function, as in

 mapslices(t -> [mean(t), median(t), iqr(t)], A, 1) 

but using concepts and splatting, as in your last example, is also great. For very large arrays, you can avoid the time distributions introduced by transposition and splatting, but in most cases you do not need to pay attention to this.

+7
source share

After playing a little, I found one option, but I'm still interested to hear if he has the best ways to do this.

 [[mapslices(f, A, 1)' for f in [mean median iqr]]...] 
+2
source share

All Articles