Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!usc!snorkelwacker.mit.edu!bloom-picayune.mit.edu!athena.mit.edu!bjaspan From: bjaspan@athena.mit.edu (Barr3y Jaspan) Newsgroups: comp.lang.c++ Subject: Re: Inline transposition of a matrix. Message-ID: <1991Apr27.193947.24409@athena.mit.edu> Date: 27 Apr 91 19:39:47 GMT References: <1991Apr26.182944.35669@slate.mines.colorado.edu> Sender: news@athena.mit.edu (News system) Organization: Massachusetts Institute of Technology Lines: 33 In article <1991Apr26.182944.35669@slate.mines.colorado.edu>, mmin@slate.mines.colorado.edu (Man-Sik Min ) writes: |> Can anybody send me a code of algorithm which transposes |> m by n matrix(read in as 1-d array). Well, here is the "transpose" method from my Matrix class. It uses two for loops and calls operator() which accesses an element. However, operator() just computes an index into a 1-d array, so from this you should be able to write code to do what you want. Matrix Matrix::T() const { Matrix t(M_n, M_m); for (int i = 1; i <= M_m; i++) for (int j = 1; j <= M_n; j++) t(j, i) = (*this)(i, j); return t; } inline double& Matrix::operator()(int m, int n) const { /* My kingdom for exception handling ... */ if (m > M_m || n > M_n || m < 1 || n < 1) abort(); return d[M_n*(m-1) + n - 1]; } -- Barr3y Jaspan, bjaspan@mit.edu