mapslices

mapslices(f, A, dims)

Transform the given dimensions of array A using function f. f is called on each slice of A of the form A[...,:,...,:,...]. dims is an integer vector specifying where the colons go in this expression. The results are concatenated along the remaining dimensions. For example, if dims is [1,2] and A is 4-dimensional, f is called on A[:,:,i,j] for all i and j.

Examples

julia> A = [1 2 3; 4 5 6; 7 8 9];

julia> mapslices(sum, A, [1])
3×1 Matrix{Int64}:
 12
 15
 18

In this example, the mapslices function is used to calculate the sum along the first dimension of the matrix A. The resulting array has dimensions 3×1 where each element represents the sum of the corresponding column in A.

julia> B = [1 2 3; 4 5 6; 7 8 9; 10 11 12];

julia> mapslices(prod, B, [2])
4×1 Matrix{Int64}:
   6
 120
 504
1320

Here, the mapslices function is used to calculate the product along the second dimension of the matrix B. The resulting array has dimensions 4×1 where each element represents the product of the corresponding row in B.

julia> C = [1 2 3; 4 5 6; 7 8 9; 10 11 12];

julia> mapslices(mean, C, [1, 2])
1×1 Matrix{Float64}:
 6.5

In this example, the mapslices function is used to calculate the mean of all elements in matrix C. The resulting array has dimensions 1×1 and contains the mean value.

julia> D = [1 2 3; 4 5 6; 7 8 9; 10 11 12];

julia> mapslices(maximum, D, [1, 2])
1-element Vector{Int64}:
 12

Here, the mapslices function is used to find the maximum value of all elements in matrix D. The resulting array is a 1-dimensional vector containing the maximum value.

Note: The examples provided assume that the Julia environment is set up and the required packages are imported if necessary.

See Also

User Contributed Notes

Add a Note

The format of note supported is markdown, use triple backtick to start and end a code block.

*Required Field
Details

Checking you are not a robot: