如何将列表变量添加到 table 框或矩阵之类的东西

How can I adding list variable to the the table box or something like matrix

我想知道如何将一些列表变量添加到单个变量,有些东西看起来像矩阵。

我会附加到什么样的......?!?

set EarthquakesNameForFactorLineNO [list]
set EarthquakesNameForFactor [list]
set FirstRow [list]
set SecondRow [list]

例如:

EarthquakesNameForFactorLineNO = [$a $b $c $d $e]
EarthquakesNameForFactor = [$f $g $h $i $g]
设置第一行 [列表] = [$k $l $m $n $o]
设置 SecondRow [列表] = [$p $q $r $s $t]

现在我需要像这样的单个变量:

[矩阵] 4*5

$a $b $c $d $e
$f $g $h $i $j
$k $l $m $n $o
$p $q $r $s $t

另外我想问一个问题... 我怎样才能创建这个矩阵的对立面,我的意思是将行更改为列,我不确定,也许在英语中称之为逆矩阵或其他东西:-)

我的意思是:

[矩阵] 5*4

$a $f $k $p
$b $g $l $q
$c $h $m $r
$d $i $n $s
$e $j $o $t

详细说明我的评论,假设您有如下 4 个列表,列表的内容是一些字母而不是变量(应该不会造成任何问题)

set EarthquakesNameForFactorLineNO [list a b c d e]
set EarthquakesNameForFactor       [list f g h i j]
set FirstRow                       [list k l m n o]
set SecondRow                      [list p q r s t]

现在,为了得到矩阵,我们将创建一个包含所有 4 个列表的列表:

set matrix1 [list $EarthquakesNameForFactorLineNO $EarthquakesNameForFactor $FirstRow $SecondRow]

# Equivalent to
# set matrix1 [list [list a b c d e] [list f g h i g] [list k l m n o] [list p q r s t]]

如果您想以 4x5 的方式查看 'matrix',只需打印列表,并用换行符连接:

puts [join $matrix1 \n]
# This displays:
# a b c d e
# f g h i g
# k l m n o
# p q r s t

然后你可以使用一些基本的循环来获得矩阵转置。这里的内部循环遍历行,而外部循环遍历列。 matrix2 将是转置矩阵,newrow 将在转置矩阵中包含当前行。由于循环的设置方式,以下将首先添加每行中的所有第一个值,然后获取每行中的所有第二个值,依此类推。

set matrix2 [list]

for {set column 0} {$column < [llength [lindex $matrix1 0]]} {incr column} {
    set newrow [list]
    foreach row $matrix1 {
        lappend newrow [lindex $row $column]
    }
    lappend matrix2 $newrow
}

puts [join $matrix2 \n]

对于上面的内容,如果你想要一个循环,你可以使用下面可能更复杂的循环:

for {set a 0; set b 0} {
    $a < [llength [lindex $matrix1 0]] && $b <= [llength $matrix1]
} {incr b} {
    if {$b == [llength $matrix1]} {
        set b -1
        incr a
        lappend matrix2 $newrow
        set newrow [list]
    } else {
        lappend newrow [lindex $matrix1 $b $a]
    }
}
puts [join $matrix2 \n]

无论你选择哪个,如果你经常使用它,你可能想把上面的放在一个过程中:

proc transpose_matrix {matrix1} {
    for {set a 0; set b 0} {
        $a < [llength [lindex $matrix1 0]] && $b <= [llength $matrix1]
    } {incr b} {
        if {$b == [llength $matrix1]} {
            set b -1
            incr a
            lappend matrix2 $newrow
            set newrow [list]
        } else {
            lappend newrow [lindex $matrix1 $b $a]
        }
    }
    return $matrix2
}

之后调用

就会得到matrix2中的转置矩阵
set matrix2 [transpose_matrix $matrix1]

请注意,如果您有长度可变的数字或单词,显示可能不会太漂亮...例如,对于其他值,矩阵可能如下所示:

1.45 1 2 310 43
0 2.3 10 20 13
342.04 11.49 87.32 0.987 2.3
3.2 12.45 11.11 43.2 35

这可能难以阅读...但是,那是另一个问题:)

package require struct::matrix

struct::matrix xdata
xdata add columns 5
xdata add rows    4

xdata set rect 0 0 {
{a b c d e}
{f g h i g}
{k l m n o}
{p q r s t}
}

join [xdata get rect 0 0 end end] \n
# a b c d e
# f g h i g
# k l m n o
# p q r s t

xdata transpose
join [xdata get rect 0 0 end end] \n
# a f k p
# b g l q
# c h m r
# d i n s
# e g o t

xdata destroy

这实现了相同的结果,但 struct::matrix 还包括矩阵转置命令