Re: Transposing a triangular matrix.
- To: mathgroup at smc.vnet.net
- Subject: [mg24337] Re: [mg24305] Transposing a triangular matrix.
- From: Rob Pratt <rpratt at email.unc.edu>
- Date: Sun, 9 Jul 2000 04:52:44 -0400 (EDT)
- Sender: owner-wri-mathgroup at wolfram.com
upperTriTranspose[lis_List]:=Table[lis[[i,k+1-i]],{k,Length[lis]},{i,k}]
upperTriTranspose[{{0.6`, 0.65`, 0.6`}, {0.75`, 0.65`}, {0.7`}}]
Rob Pratt
Department of Operations Research
The University of North Carolina at Chapel Hill
rpratt at email.unc.edu
http://www.unc.edu/~rpratt/
On Fri, 7 Jul 2000, Wen-Feng Hsiao wrote:
> 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}}