MathGroup Archive 2000

[Date Index] [Thread Index] [Author Index]

Search the Archive

Transposing a triangular matrix.

  • To: mathgroup at smc.vnet.net
  • Subject: [mg24305] Transposing a triangular matrix.
  • From: d8442803 at student.nsysu.edu.tw (Wen-Feng Hsiao)
  • Date: Fri, 7 Jul 2000 00:11:48 -0400 (EDT)
  • Organization: NSYSU
  • Sender: owner-wri-mathgroup at wolfram.com

Dear listers,

I am writing a procedure to transpose a triangular matrix. For example, 
if the input list is {{0.6`, 0.65`, 0.6`}, {0.75`, 0.65`}, {0.7`}}, then 
the output will be {{0.6}, {0.65, 0.75}, {0.6, 0.65, 0.7}}. That is, an 
input of an upper triangular matrix as below
- 0.6 0.65 0.6
  -   0.75 0.65
      -    0.7
           -
, will obtain an output of its corresponding lower triangular matrix as 
follows.
-
0.6  -
0.65 0.75 -
0.6  0.65 0.7 -

The diagonal elements can be omitted due to no needs for the subsquent 
analysis. I have written a pretty nasty version, which needs your help to 
sharpen it. Especially, please indicate me how to remove the For & While 
statements, but still remain the functionality.

Wen-Feng

------------------------------
lowerTriMatrixQ[lst_List] := Module[{len1, lenall},
      len1 = Length[lst];
      lenall = Length /@ lst;
      Return[Range[len1] == lenall]];
upperTriMatrixQ[lst_List] := Module[{len1, lenall},
      len1 = Length[lst];
      lenall = Length /@ lst;
      Return[Range[len1] == Reverse[lenall]];
      ];

triMatrixQ[lst_List] := Return[lowerTriMatrixQ[lst] || 
upperTriMatrixQ[lst]];

triTranpose[lst_?triMatrixQ] := 
    Module[{trilst = lst, len = Length[lst], i, tmp, res = {}},
      If[upperTriMatrixQ[trilst], 
        i = 1;
        While[i <= len,
          tmp = {};
          For[j = 1, j <= i, j++,
            AppendTo[tmp, First[trilst[[j]]]];
            trilst[[j]] = Rest[trilst[[j]]];];
          AppendTo[res, tmp];
          i++;
          ];
        ];
      Return[res]]; 
(*For saving space, I only list the codes for upper trianular case*)

In[11]:=
triTranpose[{{0.6`, 0.65`, 0.6`}, {0.75`, 0.65`}, {0.7`}}]
Out[11]=
{{0.6}, {0.65, 0.75}, {0.6, 0.65, 0.7}}


  • Prev by Date: Re: LogPlot != Plot[Log]
  • Next by Date: Re: LogPlot != Plot[Log]
  • Previous by thread: Re: Mathematica gives bad integral ??
  • Next by thread: Re: Transposing a triangular matrix.