×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
• Talk With Other Members
• Be Notified Of Responses
• Keyword Search
Favorite Forums
• Automated Signatures
• Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Matrix in TCL

Matrix in TCL

(OP)

Hello everyone,

Anybody knows how to read a matrix from a txt file and then print an element of a given column and row? I am looking for something like given a matrix of 3 columns and 3 rows stored in the file matrix.txt:

1 2 3
4 5 6
7 8 9

print the element in the second line and third column, i.e., 6

Thank you for your help, I am quite new in TCL and I am having some difficulties finding this information.

RE: Matrix in TCL

Hi

Just store the data in a list and access it by ( 0 based ! ) indexes :

CODE --> Tcl

set file [open "matrix.txt" r]

while { [gets $file line] >= 0 } { lappend matrix$line
}

close $file puts [lindex [lindex$matrix 1] 2] 

Feherke.
feherke.ga

RE: Matrix in TCL

(OP)
Thank you so much feherke!
The information that you put available is quite useful.

RE: Matrix in TCL

(OP)
I have another question about Matrices in TCL

Imagine that I have a matrix with four rows and two columns, like the following:

2 3
4 4
5 4
6 2

Do you have any idea how to eliminate the duplicated entries in column 2? in order to have:

2 3
4 4
6 2

My problem is that I don't know how to compare the values of column 2 (across the rows). Thanks in advance.

RE: Matrix in TCL

Hi

The easiest solution may depend on whether those two "4"s are in consecutive lines by accident, or their consecutiveness is part of the removal requirement.

This prints only lines which have a value in column 2 that was not seen before :

CODE --> Tcl

set matrix "{2 3} {4 4} {5 4} {6 2}"

for {set i 0} {$i < [llength$matrix]} {incr i} {
set seen 0
for {set j 0} {$j <$i} {incr j} {
if {[lindex [lindex $matrix$i] 1] == [lindex [lindex $matrix$j] 1]} {
set seen 1
}
}

if {! $seen} { puts [lindex$matrix $i] } }  Feherke. feherke.ga RE: Matrix in TCL (OP) Thank you for your reply Feherke. I am still have a question, what if you have the following matrix: 2 3 0.10 4 4 0.20 5 4 0.15 6 2 0.11 How do you filter these values, printing the line with the smaller value in the third column? Output: 2 3 0.10 5 4 0.15 6 2 0.11 I would like to add something like this to your previous code: if {[lindex [lindex$matrix $i] 2] <= min of the third column} Thank you for your support. RE: Matrix in TCL Hi For this kind of problems I would prefer a solution like this, in two steps : first collect all reference value ( 2nd column ) - minimum value ( 3rd column ) pairs, then output only the lines which match previously collected data. CODE --> Tcl set matrix "{2 3 0.10} {4 4 0.20} {5 4 0.15} {6 2 0.11}" array set max {} foreach line$matrix {
if {[array get max [lindex $line 1]] == "" || [lindex [array get max [lindex$line 1]] 1] > [lindex $line 2]} { set max([lindex$line 1]) [lindex $line 2] } } foreach line$matrix {
if {[lindex [array get max [lindex $line 1]] 1] == [lindex$line 2]} {
puts $line array unset max [lindex$line 1]
}
} 
This may not be the shortest nor the fastest way, but is fairly robust and flexible in my opinion.

Feherke.
feherke.ga

RE: Matrix in TCL

(OP)
Brilliant idea! Thank you for your help.

With your support in the last two days, I was able to answer to this thread that I published one week ago:

As soon as I have time, I will post the solution to be available for consulting. Thank you

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

• Talk To Other Members
• Notification Of Responses To Questions
• Favorite Forums One Click Access
• Keyword Search Of All Posts, And More...

Register now while it's still free!