Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Matrix in TCL 1

Status
Not open for further replies.

nvhuser

Programmer
Apr 10, 2015
48
DE

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.
 
Hi

Just store the data in a list and access it by ( 0 based ! ) indexes :
Code:
[b]set file[/b] [teal][[/teal][b]open[/b] [i][green]"matrix.txt"[/green][/i] r[teal]][/teal]

[b]while[/b] [teal]{ [[/teal][b]gets[/b] [navy]$file[/navy] line[teal]] >=[/teal] [purple]0[/purple] [teal]} {[/teal]
    [b]lappend[/b] matrix [navy]$line[/navy]
[teal]}[/teal]

[b]close[/b] [navy]$file[/navy]

[b]puts[/b] [teal][[/teal][b]lindex[/b] [teal][[/teal][b]lindex[/b] [navy]$matrix[/navy] [purple]1[/purple][teal]][/teal] [purple]2[/purple][teal]][/teal]

Feherke.
feherke.ga
 
Thank you so much feherke!
The information that you put available is quite useful.
 
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.
 
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:
[b]set[/b] matrix [i][green]"{2 3} {4 4} {5 4} {6 2}"[/green][/i]

[b]for[/b] [teal]{[/teal][b]set[/b] i [purple]0[/purple][teal]} {[/teal][navy]$i[/navy] [teal]< [[/teal][b]llength[/b] [navy]$matrix[/navy][teal]]} {[/teal][b]incr[/b] i[teal]} {[/teal]
    [b]set[/b] seen [purple]0[/purple]
    [b]for[/b] [teal]{[/teal][b]set[/b] j [purple]0[/purple][teal]} {[/teal][navy]$j[/navy] [teal]<[/teal] [navy]$i[/navy][teal]} {[/teal][b]incr[/b] j[teal]} {[/teal]
        [b]if[/b] [teal]{[[/teal][b]lindex[/b] [teal][[/teal][b]lindex[/b] [navy]$matrix $i[/navy][teal]][/teal] [purple]1[/purple][teal]] == [[/teal][b]lindex[/b] [teal][[/teal][b]lindex[/b] [navy]$matrix $j[/navy][teal]][/teal] [purple]1[/purple][teal]]} {[/teal]
            [b]set[/b] seen [purple]1[/purple]
        [teal]}[/teal]
    [teal]}[/teal]

    [b]if[/b] [teal]{![/teal] [navy]$seen[/navy][teal]} {[/teal]
        [b]puts[/b] [teal][[/teal][b]lindex[/b] [navy]$matrix $i[/navy][teal]][/teal]
    [teal]}[/teal]
[teal]}[/teal]


Feherke.
feherke.ga
 
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.
 
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:
[b]set[/b] matrix [i][green]"{2 3 0.10} {4 4 0.20} {5 4 0.15} {6 2 0.11}"[/green][/i]

[b]array[/b] [b]set[/b] max [teal]{}[/teal]

[b]foreach[/b] line [navy]$matrix[/navy] [teal]{[/teal]
    [b]if[/b] [teal]{[[/teal][b]array[/b] [b]get[/b] max [teal][[/teal][b]lindex[/b] [navy]$line[/navy] [purple]1[/purple][teal]]] ==[/teal] [i][green]""[/green][/i] [teal]|| [[/teal][b]lindex[/b] [teal][[/teal][b]array[/b] [b]get[/b] max [teal][[/teal][b]lindex[/b] [navy]$line[/navy] [purple]1[/purple][teal]]][/teal] [purple]1[/purple][teal]] > [[/teal][b]lindex[/b] [navy]$line[/navy] [purple]2[/purple][teal]]} {[/teal]
        [b]set[/b] max[teal]([[/teal][b]lindex[/b] [navy]$line[/navy] [purple]1[/purple][teal]]) [[/teal][b]lindex[/b] [navy]$line[/navy] [purple]2[/purple][teal]][/teal]
    [teal]}[/teal]
[teal]}[/teal]

[b]foreach[/b] line [navy]$matrix[/navy] [teal]{[/teal]
    [b]if[/b] [teal]{[[/teal][b]lindex[/b] [teal][[/teal][b]array[/b] [b]get[/b] max [teal][[/teal][b]lindex[/b] [navy]$line[/navy] [purple]1[/purple][teal]]][/teal] [purple]1[/purple][teal]] == [[/teal][b]lindex[/b] [navy]$line[/navy] [purple]2[/purple][teal]]} {[/teal]
        [b]puts[/b] [navy]$line[/navy]
        [b]array[/b] [b]unset[/b] max [teal][[/teal][b]lindex[/b] [navy]$line[/navy] [purple]1[/purple][teal]][/teal]
    [teal]}[/teal]
[teal]}[/teal]
This may not be the shortest nor the fastest way, but is fairly robust and flexible in my opinion.

Feherke.
feherke.ga
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top