Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > storing and manipulating columns of data

Reply
Thread Tools

storing and manipulating columns of data

 
 
baptiste Auguié
Guest
Posts: n/a
 
      06-30-2007
Hi,

Being used to deal with Octave / Matlab, I'm a bit confused by Ruby
structures (arrays, hashes, etc). I have this data file "data.dat"
with 3 columns:


> 1.240000 9.990000e-01 1.290000e-06
> 1.550000 1.000000e+00 2.920000e-06
> 2.066000 1.002000e+00 8.360000e-06
> 2.480000 1.001000e+00 1.520000e-05
> 2.755000 1.001000e+00 2.210000e-05
> 3.099000 1.003000e+00 3.570000e-05
> 3.444000 1.003000e+00 3.240000e-05
> 3.542000 1.003000e+00 1.720000e-05
> 4.133000 1.001000e+00 1.430000e-05
> 4.959000 1.001000e+00 2.400000e-05
> 6.199000 1.001000e+00 4.720000e-05
> 8.265000 9.990000e-01 1.210000e-04


I would like to think of the columns as arrays "a", "b", "c", execute
a loop along their indices, pick the values i want, and do something
with them. What would be a sensible data structure for this?
I can read this datafile line by line using IO:readlines("data.dat"),
but I cannot find a way to refer to one column in the array of
strings generated.


Best regards,

baptiste


 
Reply With Quote
 
 
 
 
Alex Young
Guest
Posts: n/a
 
      06-30-2007
baptiste Auguié wrote:
> Hi,
>
> Being used to deal with Octave / Matlab, I'm a bit confused by Ruby
> structures (arrays, hashes, etc). I have this data file "data.dat" with
> 3 columns:
>
>
>> 1.240000 9.990000e-01 1.290000e-06
>> 1.550000 1.000000e+00 2.920000e-06
>> 2.066000 1.002000e+00 8.360000e-06
>> 2.480000 1.001000e+00 1.520000e-05
>> 2.755000 1.001000e+00 2.210000e-05
>> 3.099000 1.003000e+00 3.570000e-05
>> 3.444000 1.003000e+00 3.240000e-05
>> 3.542000 1.003000e+00 1.720000e-05
>> 4.133000 1.001000e+00 1.430000e-05
>> 4.959000 1.001000e+00 2.400000e-05
>> 6.199000 1.001000e+00 4.720000e-05
>> 8.265000 9.990000e-01 1.210000e-04

>
> I would like to think of the columns as arrays "a", "b", "c", execute a
> loop along their indices, pick the values i want, and do something with
> them. What would be a sensible data structure for this?
> I can read this datafile line by line using IO:readlines("data.dat"),
> but I cannot find a way to refer to one column in the array of strings
> generated.

In native Ruby, the only way to do that would be:

arr = [[0,1],[2,3],[4,5]]
arr.map{|a| a[0]}
# => [0,2,4]

If you can install rb-gsl (and it's probably a good idea if you're
expecting to do matlab/octavey things), then there's the
GSL::Matrix#column method which (as I understand it, not being a heavy
GSL user) actually gives a reference into the original matrix, rather
than copying to a new one as Array#map does.

--
Alex

 
Reply With Quote
 
 
 
 
Tim Hunter
Guest
Posts: n/a
 
      06-30-2007
baptiste Auguié wrote:
> Hi,
>
> Being used to deal with Octave / Matlab, I'm a bit confused by Ruby
> structures (arrays, hashes, etc). I have this data file "data.dat"
> with 3 columns:
>
> I would like to think of the columns as arrays "a", "b", "c", execute
> a loop along their indices, pick the values i want, and do something
> with them. What would be a sensible data structure for this?
> I can read this datafile line by line using IO:readlines("data.dat"),
> but I cannot find a way to refer to one column in the array of strings
> generated.
>

You can use String#split to split each line into an array of 3 numbers.
If you want to use the array values as numbers, you'll need to use
String#to_f as well to convert the strings to numeric values.

ri String#split
ri String#to_f

--
RMagick OS X Installer [http://rubyforge.org/projects/rmagick/]
RMagick Hints & Tips [http://rubyforge.org/forum/forum.php?forum_id=1618]
RMagick Installation FAQ [http://rmagick.rubyforge.org/install-faq.html]


 
Reply With Quote
 
Morton Goldberg
Guest
Posts: n/a
 
      06-30-2007
On Jun 30, 2007, at 6:37 AM, baptiste Augui=E9 wrote:

> Being used to deal with Octave / Matlab, I'm a bit confused by Ruby =20=


> structures (arrays, hashes, etc). I have this data file "data.dat" =20
> with 3 columns:
>
>
>> 1.240000 9.990000e-01 1.290000e-06
>> 1.550000 1.000000e+00 2.920000e-06
>> 2.066000 1.002000e+00 8.360000e-06
>> 2.480000 1.001000e+00 1.520000e-05
>> 2.755000 1.001000e+00 2.210000e-05
>> 3.099000 1.003000e+00 3.570000e-05
>> 3.444000 1.003000e+00 3.240000e-05
>> 3.542000 1.003000e+00 1.720000e-05
>> 4.133000 1.001000e+00 1.430000e-05
>> 4.959000 1.001000e+00 2.400000e-05
>> 6.199000 1.001000e+00 4.720000e-05
>> 8.265000 9.990000e-01 1.210000e-04

>
> I would like to think of the columns as arrays "a", "b", "c", =20
> execute a loop along their indices, pick the values i want, and do =20
> something with them. What would be a sensible data structure for this?
> I can read this datafile line by line using IO:readlines=20
> ("data.dat"), but I cannot find a way to refer to one column in the =20=


> array of strings generated.


Maybe something like this is what you're looking for:

<code>
data =3D DATA.readlines
rows =3D data.map do |row|
row.split(/\s+/).map { |elt| elt.to_f }
end
a, b, c =3D rows.transpose
p a, b, c
__END__
1.240000 9.990000e-01 1.290000e-06
1.550000 1.000000e+00 2.920000e-06
2.066000 1.002000e+00 8.360000e-06
2.480000 1.001000e+00 1.520000e-05
</code>

<result>
[1.24, 1.55, 2.066, 2.48]
[0.999, 1.0, 1.002, 1.001]
[1.29e-06, 2.92e-06, 8.36e-06, 1.52e-05]
</result>


Regards, Morton



 
Reply With Quote
 
Axel Etzold
Guest
Posts: n/a
 
      06-30-2007
Dear Baptiste,

I'd also recommend rb-gsl, but if you have trouble
installing from source, you can also use the Matrix
class from Ruby:

http://www.ruby-doc.org/stdlib/libdo...es/Matrix.html


require "matrix"

a=Matrix.columns([[25, 93], [-1, 66]])
=> 25 -1
93 66

a.transpose
=> 25 93
-1 66

Best regards,

Axel
--
Ist Ihr Browser Vista-kompatibel? Jetzt die neuesten
Browser-Versionen downloaden: http://www.gmx.net/de/go/browser

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
User Images: Storing in Files VS Storing in Database Jonathan Wood ASP .Net 1 06-02-2008 05:56 PM
storing pointer vs storing object toton C++ 11 10-13-2006 11:08 AM
database columns vs. gui columns and sorting ittay.dror@gmail.com Java 5 03-04-2006 10:48 AM
convert rows to columns and columns to rows helpful sql ASP .Net 0 05-19-2005 06:03 PM
Using Apache::Session for storing form data as opposed to database columns ioneabu@yahoo.com Perl Misc 0 12-28-2004 06:00 PM



Advertisments