I want to know how can I add some list variable to single variable, some thing look like matrix.
llappend to what kind of ... ?!?
set EarthquakesNameForFactorLineNO [list]
set EarthquakesNameForFactor [list]
set FirstRow [list]
set SecondRow [list]
for example :
EarthquakesNameForFactorLineNO = [$a $b $c $d $e] EarthquakesNameForFactor = [$f $g $h $i $g] set FirstRow [list] = [$k $l $m $n $o] set SecondRow [list] = [$p $q $r $s $t]
Now I need single variable like this:
[MATRIX] 4*5 $a $b $c $d $e $f $g $h $i $j $k $l $m $n $o $p $q $r $s $t
and also I want to ask another question... how can I create the opposite of this matrix, I mean change row to column, I'm not sure, perhaps in English call it inverse or something else :-)
I mean this:
[MATRIX] 5*4 $a $f $k $p $b $g $l $q $c $h $m $r $d $i $n $s $e $j $o $t
Elaborating on my comment, let's say you have the 4 lists as below, with the contents of the lists some letters instead of variables (that shouldn't pose any issue)
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]
Now, to get the matrix, we will make a list containing all the 4 lists:
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]]
If you want to see that 'matrix' in a 4x5 fashion, you just need to print the list, joined by a newline:
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
Then you can use some basic looping to get the matrix transposed. The inner loop here is going through the rows, while the outer loops is going through the columns. matrix2
will be the transposed matrix and newrow
will have the current row in the transposed matrix. Due to the way the loop was set up, the following will add all the first values in each row first, then take all the second values in each row, and so on.
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 the above, if you want a single loop, you can use the below possibly more complex loop:
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]
Whichever, you choose, if you are using this a lot, you may want to put the above in a proc:
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
}
After which you will get the transposed matrix in matrix2
whenever you call
set matrix2 [transpose_matrix $matrix1]
Note that if you have numbers or words that are variable in length, the display might not be too pretty... for instance, with other values, the matrix might look like:
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
Which can be hard to read... but then, that's a different problem :)