Re: storing and manipulating columns of data



On Jun 30, 2007, at 6:37 AM, baptiste Auguié wrote:

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.

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

<code>
data = DATA.readlines
rows = data.map do |row|
row.split(/\s+/).map { |elt| elt.to_f }
end
a, b, c = 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



.



Relevant Pages

  • Re: Whats this C code?
    ... this type of arrays ... Usually an attacker exploits some unprotected buffers, ... OS to execute arbitrary code. ... This is the cornerstone documentation, ...
    (Security-Basics)
  • Re: How do I assign values to an array?
    ... but for a 5-entry array of singles those drags ... > large arrays, in which case the function call and processing overhead would ... procedure, which takes 200 nannoseconds to execute, could, with some ...
    (microsoft.public.excel.programming)
  • Re: Dictionary, hashing limitations
    ... length limitation). ... And of course lots of target compilers won't have a dictionary on the ... There may not be much value in allowing associative arrays whose values ... have to know what type they are; you can execute them and their own code ...
    (comp.lang.forth)
  • Re: segmentation fault
    ... I was able to locate the problem by first making the arrays ... This allowed the program to execute long enough for me ... Kevin say. ...
    (comp.lang.fortran)
  • Error due to large array?
    ... when i try to execute the following program i get an error saying: ... the system can not execute the specified program. ... smaller (all arrays less than about 5000), ...
    (comp.lang.fortran)