read matrix file into 2d array

Z

zhong huang

Could anyone help me?

I have a matrix file (matrix.dat) which looks like this:

0.97 0.97 0.97 0.81 0.8 0.53 0.54 0.54 0.36 0.41 0.5 0.46 0.61 0.86
0.95 0.7 0.86 0.81 0.57 0.55 0.55 0.46 0.46 0.47 0.63 0.7 0.9 1.05
0.98 0.88 0.86 0.89 0.85 0.57 0.6 0.49 0.46 0.37 0.53 0.43 0.79 1.06
0.87 0.67 1 0.71 0.79 0.5 0.51 0.69 0.45 0.63 0.61 0.58 0.78 1.06
1.14 0.69 1.23 0.63 0.92 0.6 0.46 0.55 0.32 0.38 0.46 0.47 0.68 0.82
1.16 0.77 1.02 0.8 0.85 0.47 0.54 0.55 0.37 0.43 0.57 0.53 0.64 0.87
1.15 0.81 0.89 0.67 0.77 0.48 0.59 0.48 0.4 0.34 0.51 0.5 0.59 0.91

I want to open the file and put each element in a 2d array. My code is
like this:

==code start==
$file = "matrix.dat";
open(FILE,$file);
@data = <FILE>;

#each line is an element of the @data, loop through and separate each row in a bigger 2d array

for ($m=0;$m<scalar(@data)-1;++$m){
while($data[$m]){
for $j(0..13){ #there are 14 elements in each row
$td_array[$m][$j] = [split('\t',$data[$m]);
}
}

print "@td_array;
==code end==

It doesn't work. A working script is much appreciated.


Zhong
 
J

John J. Trammell

I have a matrix file (matrix.dat) which looks like this:
[snip]

I want to open the file and put each element in a 2d array. My code is
like this:

==code start==
$file = "matrix.dat";
open(FILE,$file);
@data = <FILE>;

#each line is an element of the @data, loop through and separate each
row in a bigger 2d array

for ($m=0;$m<scalar(@data)-1;++$m){
while($data[$m]){
for $j(0..13){ #there are 14 elements in each row
$td_array[$m][$j] = [split('\t',$data[$m]);
}
}

print "@td_array;
==code end==

It doesn't work. A working script is much appreciated.

How about (untested):

my $file = "matrix.dat";
open(FILE,$file) or die "can't open $file: $!";
my @data;
while (<FILE>) {
my @row = split ' ', $_;
push @data, \@row;
}
use Data::Dumper;
print Dumper(\@row);

Ain't Perl great?
 
U

Uri Guttman

JJT> How about (untested):

JJT> my $file = "matrix.dat";
JJT> open(FILE,$file) or die "can't open $file: $!";
JJT> my @data;
JJT> while (<FILE>) {
JJT> my @row = split ' ', $_;
JJT> push @data, \@row;
JJT> }
JJT> use Data::Dumper;
JJT> print Dumper(\@row);

that should be \@data i think.

but to show perl's power look at this:

my @data = map [ split ], <FILE> ;

and if you use the module file::slurp (soon to be a major upgrade) then
you can remove the open too:

use File::Slurp ;
my @data = map [ split ], read_file( $file ) ;

makes loading data like this a snap.

if the file was passed in on the command line you can even go with:

my @data = map [ split ], <> ;

perl has such good default behavior for common and simple stuff like this.

JJT> Ain't Perl great?

yeah :)

uri
 
J

John W. Krahn

zhong said:
Could anyone help me?

I have a matrix file (matrix.dat) which looks like this:

0.97 0.97 0.97 0.81 0.8 0.53 0.54 0.54 0.36 0.41 0.5 0.46 0.61 0.86
0.95 0.7 0.86 0.81 0.57 0.55 0.55 0.46 0.46 0.47 0.63 0.7 0.9 1.05
0.98 0.88 0.86 0.89 0.85 0.57 0.6 0.49 0.46 0.37 0.53 0.43 0.79 1.06
0.87 0.67 1 0.71 0.79 0.5 0.51 0.69 0.45 0.63 0.61 0.58 0.78 1.06
1.14 0.69 1.23 0.63 0.92 0.6 0.46 0.55 0.32 0.38 0.46 0.47 0.68 0.82
1.16 0.77 1.02 0.8 0.85 0.47 0.54 0.55 0.37 0.43 0.57 0.53 0.64 0.87
1.15 0.81 0.89 0.67 0.77 0.48 0.59 0.48 0.4 0.34 0.51 0.5 0.59 0.91

I want to open the file and put each element in a 2d array. My code is
like this:

==code start==
$file = "matrix.dat";
open(FILE,$file);
@data = <FILE>;

#each line is an element of the @data, loop through and separate each row in a bigger 2d array

for ($m=0;$m<scalar(@data)-1;++$m){
while($data[$m]){
for $j(0..13){ #there are 14 elements in each row
$td_array[$m][$j] = [split('\t',$data[$m]);
}
}

print "@td_array;
==code end==

It doesn't work. A working script is much appreciated.

This is probably what you want:

use warnings;
use strict;
use Data::Dumper;

my $file = 'matrix.dat';
open FILE, $file or die "Cannot open $file: $!";
my @td_array = map [ split ], <FILE>;

print Dumper( \@td_array );

__END__



John
 
T

Tad McClellan

Uri Guttman said:
and if you use the module file::slurp (soon to be a major upgrade) then
you can remove the open too:

use File::Slurp ;
my @data = map [ split ], read_file( $file ) ;

makes loading data like this a snap.

if the file was passed in on the command line you can even go with:

my @data = map [ split ], <> ;


And if it is not passed on the command line and you don't have
File::Slurp, you can still get it done pretty easily:

my @data;
{ local @ARGV = $file;
@data = map [ split ], <> ;
}


:)


JJT> Ain't Perl great?

yeah :)


And all the congregation said "Amen!"...
 
D

David

zhong huang said:
I have a matrix file (matrix.dat) which looks like this: [snip]
I want to open the file and put each element in a 2d array.

Hi Zhong,

Being as you're most likely a C programmer (the for-loop with prefix
incrementing was a dead give away!), here's some code that's decidedly
non-C that'll do the trick of reading in your matrix:

my $file = 'matrix.dat';
my @data;

open( FILE, $file ) or die "Can't open file '$file': $!";
while( <FILE> ) {
chomp;
my @row = split;
push @data, \@row;
}
close( FILE );

# Access data with $data[row][col], noting
# that row and col are indexed at zero, not one
print 'Cell (0, 0) = ', $data[0][0], "\n";
print 'Cell (1, 3) = ', $data[1][3], "\n";
print 'Cell (3, 4) = ', $data[3][4], "\n";

HTH,
David

P.S. You wouldn't happen to be on the TJU Medical College's admissions
committee, would you? Maybe you could help pass my application
along... :) (Probably not, but hey, it was worth a shot!)
 
B

Bob Walton

zhong said:
Could anyone help me?

I have a matrix file (matrix.dat) which looks like this:

0.97 0.97 0.97 0.81 0.8 0.53 0.54 0.54 0.36 0.41 0.5 0.46 0.61 0.86
0.95 0.7 0.86 0.81 0.57 0.55 0.55 0.46 0.46 0.47 0.63 0.7 0.9 1.05
0.98 0.88 0.86 0.89 0.85 0.57 0.6 0.49 0.46 0.37 0.53 0.43 0.79 1.06
0.87 0.67 1 0.71 0.79 0.5 0.51 0.69 0.45 0.63 0.61 0.58 0.78 1.06
1.14 0.69 1.23 0.63 0.92 0.6 0.46 0.55 0.32 0.38 0.46 0.47 0.68 0.82
1.16 0.77 1.02 0.8 0.85 0.47 0.54 0.55 0.37 0.43 0.57 0.53 0.64 0.87
1.15 0.81 0.89 0.67 0.77 0.48 0.59 0.48 0.4 0.34 0.51 0.5 0.59 0.91

I want to open the file and put each element in a 2d array. My code is
like this:


You might want to check out the Math::MatrixReal module (there are
examples in its docs of how to read a matrix from a file). It will
probably greatly simplify life for whatever you are intending to do with
the matrix once you have read it.


....
 
U

Uri Guttman

TM> And if it is not passed on the command line and you don't have
TM> File::Slurp, you can still get it done pretty easily:

TM> my @data;
TM> { local @ARGV = $file;
TM> @data = map [ split ], <> ;
TM> }

double blech!!

my @data = do { local @ARGV = $file ; map [ split ], <> } ;

:)

uri
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,968
Messages
2,570,154
Members
46,702
Latest member
LukasConde

Latest Threads

Top