Benjohn
Benjohn

Reputation: 13887

Map over slices

Julia's map and comprehension syntax make it easy to map over all elements of a multidimensional array.

Is there a similar support for mapping over slices of an array?

As a silly example, given a 3x3x100 matrix, I might want to map over all 100 3x3x_ slices. I might, say, derive the determinant of each 3x3 slice, and end up with a 1x1x100 array of determinants.

Upvotes: 1

Views: 854

Answers (1)

Dan Getz
Dan Getz

Reputation: 18217

Look at mapslices. For the question suggest an example with size(A)==(3,3,100). Calculating the 100 determinantst of 3x3 matrices can be done with: mapslices(det,A,(1,2)).

Note the resulting matrix is still 3 dimensional, and squeeze can be used to get rid of the size 1 dimensions. In the example:

squeeze(mapslices(det,A,(1,2)),(1,2))

Upvotes: 2

Related Questions