## 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

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

## CODE --> Tcl

Feherke.

feherke.ga

## RE: Matrix in TCL

The information that you put available is quite useful.

## RE: Matrix 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

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

Feherke.

feherke.ga

## RE: Matrix in TCL

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

For this kind of problems I would prefer a solution like this, in two steps : first collect all reference value ( 2

^{nd}column ) - minimum value ( 3^{rd}column ) pairs, then output only the lines which match previously collected data.## CODE --> Tcl

Feherke.

feherke.ga

## RE: Matrix in TCL

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

http://www.tek-tips.com/viewthread.cfm?qid=1748068

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