matrix invert routine

Arnold D. Robbins {EUCC} arnold at mathcs.emory.edu
Sat Nov 11 05:19:25 AEST 1989


>In article <1612 at xn.LL.MIT.EDU>, rkc at XN.LL.MIT.EDU (rkc) writes:
>> I have "spreadsheet-like" data that looks like this:
>> 	a1 b1 c1
>> 	a2 b2 c2
>> 	a3 b3 c3
>> and I want to get it in a form like:
>> 	a1 a2 a3
>> 	b1 b2 b3
>> 	c1 c2 c3

In article <2560 at ucsfcca.ucsf.edu> root at cca.ucsf.edu (Systems Staff) writes:
>the transpose with standard Unix tools (I would look at awk first) ....

Aha! No sooner said than done.  From the 2.11 gawk.texinfo manual:

-- Begin Quote --
The following example treats its input as a two-dimensional array of
fields; it rotates this array 90 degrees clockwise and prints the
result.  It assumes that all lines have the same number of
elements.

	awk '{
	     if (max_nf < NF)
	          max_nf = NF
	     max_nr = NR
	     for (x = 1; x <= NF; x++)
	          vector[x, NR] = $x
	}
	
	END {
	     for (x = 1; x <= max_nf; x++) {
	          for (y = max_nr; y >= 1; --y)
	               printf("%s ", vector[x, y])
	          printf("\n")
	     }
	}'


When given the input:

	1 2 3 4 5 6
	2 3 4 5 6 1
	3 4 5 6 1 2
	4 5 6 1 2 3

it produces:

	4 3 2 1
	5 4 3 2
	6 5 4 3
	1 6 5 4
	2 1 6 5
	3 2 1 6

-- End Quote --
-- 
Arnold Robbins -- Emory U. Information Technology Div.  | Laundry increases
DOMAIN: arnold at emoryu1.cc.emory.edu			| exponentially in the
UUCP: gatech!emoryu1!arnold  PHONE: +1 404 727-7636	| number of children.
BITNET: arnold at emoryu1	     FAX:   +1 404 727-2599	|   -- Miriam Hartholz



More information about the Comp.unix.questions mailing list