Dear aal, Is there a way to rotate array or a cube of matrices by Y axis? MatLab example: A = cat(3,{'a' 'b' 'c';'d' 'e' 'f';'g' 'h' 'i'},{'j' 'k' 'l';'m' 'n' 'o';'p' 'q' 'r'}) A(:,:,1) 'a' 'b' 'c' 'd' 'e' 'f' 'g' 'h' 'i' A(:,:,2) 'j' 'k' 'l' 'm' 'n' 'o' 'p' 'q' 'r' Rotate the cell array by 270 degrees. B = rot90(A,3) B(:,:,1) 'g' 'd' 'a' 'h' 'e' 'b' 'i' 'f' 'c' B(:,:,2) 'p' 'm' 'j' 'q' 'n' 'k' 'r' 'o' 'l' karim [[alternative HTML version deleted]]
?aperm aperm(A, c(2,1,3) )[,3:1,] --------------------------------------------------------------------------- Jeff Newmiller The ..... ..... Go Live... DCN:<jdnewmil at dcn.davis.ca.us> Basics: ##.#. ##.#. Live Go... Live: OO#.. Dead: OO#.. Playing Research Engineer (Solar/Batteries O.O#. #.O#. with /Software/Embedded Controllers) .OO#. .OO#. rocks...1k --------------------------------------------------------------------------- Sent from my phone. Please excuse my brevity. On February 5, 2015 1:17:43 PM PST, Karim Mezhoud <kmezhoud at gmail.com> wrote:>Dear aal, > >Is there a way to rotate array or a cube of matrices by Y axis? > > >MatLab example: > >A = cat(3,{'a' 'b' 'c';'d' 'e' 'f';'g' 'h' 'i'},{'j' 'k' 'l';'m' 'n' >'o';'p' 'q' 'r'}) > >A(:,:,1) > > 'a' 'b' 'c' > 'd' 'e' 'f' > 'g' 'h' 'i' > > >A(:,:,2) > > 'j' 'k' 'l' > 'm' 'n' 'o' > 'p' 'q' 'r' > >Rotate the cell array by 270 degrees. > >B = rot90(A,3) > >B(:,:,1) > > 'g' 'd' 'a' > 'h' 'e' 'b' > 'i' 'f' 'c' > > >B(:,:,2) > > 'p' 'm' 'j' > 'q' 'n' 'k' > 'r' 'o' 'l' > > >karim > > [[alternative HTML version deleted]] > >______________________________________________ >R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see >https://stat.ethz.ch/mailman/listinfo/r-help >PLEASE do read the posting guide >http://www.R-project.org/posting-guide.html >and provide commented, minimal, self-contained, reproducible code.
Hello, Try the following. rot90 <- function(x, n = 1){ r90 <- function(x){ y <- matrix(rep(NA, prod(dim(x))), nrow = nrow(x)) for(i in seq_len(nrow(x))) y[, i] <- rev(x[i, ]) y } for(i in seq_len(n)) x <- r90(x) x } mat <- matrix(letters[1:9], byrow = TRUE, nrow = 3) rot90(mat) rot90(mat, 3) Hope this helps, Rui Barradas Em 05-02-2015 21:17, Karim Mezhoud escreveu:> Dear aal, > > Is there a way to rotate array or a cube of matrices by Y axis? > > > MatLab example: > > A = cat(3,{'a' 'b' 'c';'d' 'e' 'f';'g' 'h' 'i'},{'j' 'k' 'l';'m' 'n' > 'o';'p' 'q' 'r'}) > > A(:,:,1) > > 'a' 'b' 'c' > 'd' 'e' 'f' > 'g' 'h' 'i' > > > A(:,:,2) > > 'j' 'k' 'l' > 'm' 'n' 'o' > 'p' 'q' 'r' > > Rotate the cell array by 270 degrees. > > B = rot90(A,3) > > B(:,:,1) > > 'g' 'd' 'a' > 'h' 'e' 'b' > 'i' 'f' 'c' > > > B(:,:,2) > > 'p' 'm' 'j' > 'q' 'n' 'k' > 'r' 'o' 'l' > > > karim > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >
Thanks Jeff and Rui. On Thu, Feb 5, 2015 at 11:12 PM, Rui Barradas <ruipbarradas at sapo.pt> wrote:> Hello, > > Try the following. > > > rot90 <- function(x, n = 1){ > r90 <- function(x){ > y <- matrix(rep(NA, prod(dim(x))), nrow = nrow(x)) > for(i in seq_len(nrow(x))) y[, i] <- rev(x[i, ]) > y > } > for(i in seq_len(n)) x <- r90(x) > x > } > > mat <- matrix(letters[1:9], byrow = TRUE, nrow = 3) > > rot90(mat) > rot90(mat, 3) > > > Hope this helps, > > Rui Barradas > > Em 05-02-2015 21:17, Karim Mezhoud escreveu: > >> Dear aal, >> >> Is there a way to rotate array or a cube of matrices by Y axis? >> >> >> MatLab example: >> >> A = cat(3,{'a' 'b' 'c';'d' 'e' 'f';'g' 'h' 'i'},{'j' 'k' 'l';'m' 'n' >> 'o';'p' 'q' 'r'}) >> >> A(:,:,1) >> >> 'a' 'b' 'c' >> 'd' 'e' 'f' >> 'g' 'h' 'i' >> >> >> A(:,:,2) >> >> 'j' 'k' 'l' >> 'm' 'n' 'o' >> 'p' 'q' 'r' >> >> Rotate the cell array by 270 degrees. >> >> B = rot90(A,3) >> >> B(:,:,1) >> >> 'g' 'd' 'a' >> 'h' 'e' 'b' >> 'i' 'f' 'c' >> >> >> B(:,:,2) >> >> 'p' 'm' 'j' >> 'q' 'n' 'k' >> 'r' 'o' 'l' >> >> >> karim >> >> [[alternative HTML version deleted]] >> >> ______________________________________________ >> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see >> https://stat.ethz.ch/mailman/listinfo/r-help >> PLEASE do read the posting guide http://www.R-project.org/ >> posting-guide.html >> and provide commented, minimal, self-contained, reproducible code. >> >>[[alternative HTML version deleted]]
Hi, Here is an implementation.> rot90function(a,times=1) { row <- dim(a)[1] col <- dim(a)[2] dep <- dim(a)[3] if (times %% 2 == 1){t <- row; row <- col; col <- t} tempA <- array(NA, c(row,col,dep)) for (i in 1:dep) { temp <- a[,,i] for (j in 1:times) { temp <- cbind(sapply(1:row,function(x){rev(temp[x,])})) } tempA[,,i] <- temp } return(tempA) }> A, , 1 [,1] [,2] [,3] [1,] "a" "b" "c" [2,] "d" "e" "f" [3,] "g" "h" "i" , , 2 [,1] [,2] [,3] [1,] "j" "k" "l" [2,] "m" "n" "o" [3,] "p" "q" "r"> rot90(A,3), , 1 [,1] [,2] [,3] [1,] "g" "d" "a" [2,] "h" "e" "b" [3,] "i" "f" "c" , , 2 [,1] [,2] [,3] [1,] "p" "m" "j" [2,] "q" "n" "k" [3,] "r" "o" "l" -- View this message in context: http://r.789695.n4.nabble.com/Rotate-array-by-90-tp4702862p4702870.html Sent from the R help mailing list archive at Nabble.com.
> lapply(1:2, function(x) t(A[rev(1:3),,x]))[[1]] [,1] [,2] [,3] [1,] "g" "d" "a" [2,] "h" "e" "b" [3,] "i" "f" "c" [[2]] [,1] [,2] [,3] [1,] "p" "m" "j" [2,] "q" "n" "k" [3,] "r" "o" "l" > Is this what you are looking for? I hope this helps. Chel Hee Lee On 02/05/2015 03:17 PM, Karim Mezhoud wrote:> Dear aal, > > Is there a way to rotate array or a cube of matrices by Y axis? > > > MatLab example: > > A = cat(3,{'a' 'b' 'c';'d' 'e' 'f';'g' 'h' 'i'},{'j' 'k' 'l';'m' 'n' > 'o';'p' 'q' 'r'}) > > A(:,:,1) > > 'a' 'b' 'c' > 'd' 'e' 'f' > 'g' 'h' 'i' > > > A(:,:,2) > > 'j' 'k' 'l' > 'm' 'n' 'o' > 'p' 'q' 'r' > > Rotate the cell array by 270 degrees. > > B = rot90(A,3) > > B(:,:,1) > > 'g' 'd' 'a' > 'h' 'e' 'b' > 'i' 'f' 'c' > > > B(:,:,2) > > 'p' 'm' 'j' > 'q' 'n' 'k' > 'r' 'o' 'l' > > > karim > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >
Thanks Lee and Huang. That is useful. Best On Fri, Feb 6, 2015 at 1:53 AM, Chel Hee Lee <chl948 at mail.usask.ca> wrote:> > lapply(1:2, function(x) t(A[rev(1:3),,x])) > [[1]] > [,1] [,2] [,3] > [1,] "g" "d" "a" > [2,] "h" "e" "b" > [3,] "i" "f" "c" > > [[2]] > [,1] [,2] [,3] > [1,] "p" "m" "j" > [2,] "q" "n" "k" > [3,] "r" "o" "l" > > > > > Is this what you are looking for? I hope this helps. > > Chel Hee Lee > > > On 02/05/2015 03:17 PM, Karim Mezhoud wrote: > >> Dear aal, >> >> Is there a way to rotate array or a cube of matrices by Y axis? >> >> >> MatLab example: >> >> A = cat(3,{'a' 'b' 'c';'d' 'e' 'f';'g' 'h' 'i'},{'j' 'k' 'l';'m' 'n' >> 'o';'p' 'q' 'r'}) >> >> A(:,:,1) >> >> 'a' 'b' 'c' >> 'd' 'e' 'f' >> 'g' 'h' 'i' >> >> >> A(:,:,2) >> >> 'j' 'k' 'l' >> 'm' 'n' 'o' >> 'p' 'q' 'r' >> >> Rotate the cell array by 270 degrees. >> >> B = rot90(A,3) >> >> B(:,:,1) >> >> 'g' 'd' 'a' >> 'h' 'e' 'b' >> 'i' 'f' 'c' >> >> >> B(:,:,2) >> >> 'p' 'm' 'j' >> 'q' 'n' 'k' >> 'r' 'o' 'l' >> >> >> karim >> >> [[alternative HTML version deleted]] >> >> ______________________________________________ >> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see >> https://stat.ethz.ch/mailman/listinfo/r-help >> PLEASE do read the posting guide http://www.R-project.org/ >> posting-guide.html >> and provide commented, minimal, self-contained, reproducible code. >> >>[[alternative HTML version deleted]]