Skip to content
Prev 369544 / 398503 Next

Reversing one dimension of an array, in a generalized case

How about this:

f <- function(a,wh){ ## a is the array; wh is the index to be reversed
   l<- lapply(dim(a),seq_len)
   l[[wh]]<- rev(l[[wh]])
   do.call(`[`,c(list(a),l))
}

## test
z <- array(1:120,dim=2:5)

##  I omit the printouts

f(z,2)

f(z,3)


Cheers,
Bert

Bert Gunter

"The trouble with having an open mind is that people keep coming along
and sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )


On Thu, Jun 1, 2017 at 9:51 AM, Roy Mendelssohn - NOAA Federal
<roy.mendelssohn at noaa.gov> wrote: