[R] Rotate array by 90°

Rui Barradas ruipbarradas at sapo.pt
Thu Feb 5 23:12:17 CET 2015


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.
>



More information about the R-help mailing list