Path: utzoo!mnetor!uunet!husc6!cmcl2!rutgers!nysernic!weltyc From: weltyc@nysernic (Christopher A. Welty) Newsgroups: comp.emacs Subject: Re: transpose,rotate,mirror,reverse text... Message-ID: <574@nysernic> Date: 10 Dec 87 21:59:29 GMT References: <8129@steinmetz.steinmetz.UUCP> Sender: nobody@nysernic Reply-To: weltyc@nisc.nyser.net.UUCP (Christopher A. Welty) Organization: RPI Computer Science Dept. Lines: 69 In article <8129@steinmetz.steinmetz.UUCP> nieh@moose.steinmetz (nico nieh) writes: > >The following problems occurred to me yesterday while I was editing >a file which contains matrices. > >1. Is there an easy way to transpose a matrix in GNU-Emacs ? > > 1 2 3 4 5 1 6 2 1 9 > 6 7 8 9 2 2 7 4 3 8 > 2 4 6 8 0 ====> 3 8 6 5 7 > 1 3 5 7 9 4 9 8 7 6 > 9 8 7 6 5 5 2 0 9 5 > Since emacs supports its own LISP, you can certainly write such fuinctions quite easily. In fact, I wrote this opne up quickly as an example. IT IS BY NMO MEANS EFFICIENT, there are a lot better ways to do it, I just wrote it real quick. If you position point at the beginning of a matrix which consists of elements separated by whitespace, and the last line of the matrix is followed by an empty line, this will put the transposed matrix after the original one. ie start with 1 2 3 3 2 1 1 2 3 and end up with 1 2 3 3 2 1 1 2 3 1 3 1 2 2 2 3 1 3 Here it is: (defun transpose () ; from point to the first blank line is current matrix (interactive) (let ((bol) (matrix nil) (i 0) (len) (tmatrix nil)) (while (not (eolp)) (setq bol (point)) (end-of-line) (setq matrix (append matrix (list (car (read-from-string (concat "(" (buffer-substring bol (point)) ")" )))))) (forward-line 1)) ; Now matrix is a list of lists, each sublist is a row (setq len (length (car matrix))) (while (< i len) (setq tmatrix (append tmatrix (list (mapcar '(lambda (x) (nth i x)) matrix)))) (setq i (1+ i))) ; tmatrix is now the transformed matrix, with each sublist a row ; Now we print it out after the current matrix (mapcar '(lambda (x) (newline 1) (mapcar '(lambda (x) (insert (format "%s " x))) x)) tmatrix))) Christopher Welty --- Asst. Director, RPI CS Labs weltyc@cs.rpi.edu ...!rutgers!nysernic!weltyc