From: tom.rmadilo on
On Jul 28, 11:37 pm, ashneerson <ashneer...(a)gmail.com> wrote:
> On Jul 29, 11:23 am, Harald Oehlmann <wortka...(a)yahoo.de> wrote:> On 29 Jul., 07:52, ashneerson <ashneer...(a)gmail.com> wrote:
>
> > > hi, have a puzzle with rotating a list like below
>
> > > {{ht a3 pr2} {ht a2 pr1} {lt a1 pr2} {lt a3 pr4} {ht a2 pr2}}
>
> > > into a list like this one:
>
> > > {{ht ht ht lt lt}  {a2 a2 a1 a3}  {pr1 pr2 pr2 pr2 pr4}}
>
> > > so that to rotate the columns to rows and retain original
> > > correspondence between the list elements.
>
> > Sorry, I did not get what you mean. Could you write it with indexes ?
>
> ok. What I want to do is convert columns to rows as follows:
>
> {
> {0 1 2}
> {0 1 3}
> {0 2 1}
> {1 1 2}
> {1 2 3}
>
> }
>
> converted to :
>
> {
> {0 0 0 1 1}
> {1 1 2 1 2}
> {2 3 1 2 3}
>
> }
>
> # initial code
> proc rotate_list { list } {
>    set lWidth [llength $list]
>    set lHeight  [llength [lindex $list 0]]
>
>    set w $lHeight
>    set h $lWidth
>
>    set list {}
>
>    for {set i 0} {$i < h} {incr i} {
>       for {set j 0} {$j < w} {incr j} {
>           list [i][j] = list[w - j -1] [i]
>       }
>    }
>    return list
>
> }
>
> what I am stuck at is this line:
>
> list [i][j] = list[w - j -1] [i]
>
> thanks!
> -a.

I wrote a proc for this, for some reason called it matrix:


proc ::resource::matrix { inList outList cols } {

# procedure creates new list from input list
# cols is the result set cols == input rows
# cols is an artificial dimension, which along
# with the length of the list determines the
# dims of the matrix. The list is filled
# with {} until the matrix is complete:
# 1 2 3 4 1 5 9
# 5 6 7 8 - cols = 3 -> 2 6 10
# 9 10 11 {} 3 7 11
# 4 8 {}
#
upvar $inList IL
upvar $outList OL

set rows [expr round(ceil([llength $IL].0/$cols))]
# make matrix
set Index [expr ($rows * $cols)]
set OL [list]

for {set i 0} {$i < $Index} {incr i} {
set x [expr $i/$cols ]
set y [expr $i % $cols]
# flip x y
lappend OL [lindex $IL [expr $y * $rows + $x] ]
}

}

Link to original source:
http://junom.com/gitweb/gitweb.perl?p=tnt.git;a=blob;f=packages/resource/tcl/resource-procs.tcl;h=45752;hb=HEAD#l100

Of course I assume you should agree that matrix dimensions are
imaginary projections of a simple list...but why choose a complex
structure and then try to manipulate it into a different form? This
choice could require a mapping for each matrix element.
From: tom.rmadilo on
On Jul 30, 11:57 pm, "tom.rmadilo" <tom.rmad...(a)gmail.com> wrote:
> On Jul 28, 11:37 pm, ashneerson <ashneer...(a)gmail.com> wrote:
>
>
>
>
>
> > On Jul 29, 11:23 am, Harald Oehlmann <wortka...(a)yahoo.de> wrote:> On 29 Jul., 07:52, ashneerson <ashneer...(a)gmail.com> wrote:
>
> > > > hi, have a puzzle with rotating a list like below
>
> > > > {{ht a3 pr2} {ht a2 pr1} {lt a1 pr2} {lt a3 pr4} {ht a2 pr2}}
>
> > > > into a list like this one:
>
> > > > {{ht ht ht lt lt}  {a2 a2 a1 a3}  {pr1 pr2 pr2 pr2 pr4}}
>
> > > > so that to rotate the columns to rows and retain original
> > > > correspondence between the list elements.
>
> > > Sorry, I did not get what you mean. Could you write it with indexes ?
>
> > ok. What I want to do is convert columns to rows as follows:
>
> > {
> > {0 1 2}
> > {0 1 3}
> > {0 2 1}
> > {1 1 2}
> > {1 2 3}
>
> > }
>
> > converted to :
>
> > {
> > {0 0 0 1 1}
> > {1 1 2 1 2}
> > {2 3 1 2 3}
>
> > }
>
> > # initial code
> > proc rotate_list { list } {
> >    set lWidth [llength $list]
> >    set lHeight  [llength [lindex $list 0]]
>
> >    set w $lHeight
> >    set h $lWidth
>
> >    set list {}
>
> >    for {set i 0} {$i < h} {incr i} {
> >       for {set j 0} {$j < w} {incr j} {
> >           list [i][j] = list[w - j -1] [i]
> >       }
> >    }
> >    return list
>
> > }
>
> > what I am stuck at is this line:
>
> > list [i][j] = list[w - j -1] [i]
>
> > thanks!
> > -a.
>
> I wrote a proc for this, for some reason called it matrix:
>
> proc ::resource::matrix { inList outList cols } {
>
>     # procedure creates new list from input list
>     # cols is the result set cols == input rows
>     # cols is an artificial dimension, which along
>     # with the length of the list determines the
>     # dims of the matrix. The list is filled
>     # with {} until the matrix is complete:
>     # 1  2  3  4                1  5  9
>     # 5  6  7  8  - cols = 3 -> 2  6 10
>     # 9 10 11 {}                3  7 11
>     #                           4  8 {}
>     #
>     upvar $inList IL
>     upvar $outList OL
>
>     set rows [expr round(ceil([llength $IL].0/$cols))]
>     # make matrix
>     set Index [expr ($rows * $cols)]
>     set OL [list]
>
>     for {set i 0} {$i < $Index} {incr i} {
>         set x [expr  $i/$cols ]
>         set y [expr  $i % $cols]
>         # flip x y
>         lappend OL [lindex $IL [expr $y * $rows + $x] ]
>     }
>
> }
>
> Link to original source:http://junom.com/gitweb/gitweb.perl?p=tnt.git;a=blob;f=packages/resou...
>
> Of course I assume you should agree that matrix dimensions are
> imaginary projections of a simple list...but why choose a complex
> structure and then try to manipulate it into a different form? This
> choice could require a mapping for each matrix element.

Maybe I should clarify: in Tcl there is no way to identify the
dimensions of a matrix, in Tcl every matrix has one row and the number
of columns is equal to the number of list elements. If you think
otherwise you are bound to fail. In Tcl multi-dimensional arrays or
matrices are a construction of the program's interpretation of the
data. So basically you can't transpose a matrix in Tcl unless you
specify the matrix dimensions (you can't just point to the matrix).

Maybe tcllib's matrix command is the best option.