# 2D array strategies

**URL:** <https://rubytalk.org/t/2d-array-strategies/66405>\
**Category:** ruby-talk\
**Created:** [2 October 2012 18:49 UTC](https://rubytalk.org/t/2d-array-strategies/66405 "2012-10-02T18:49:05Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![7stud2](https://avatars.discourse-cdn.com/v4/letter/7/9de053/32.png) [@7stud2](https://rubytalk.org/u/7stud2)\
**Post date:** [2 October 2012 18:49 UTC](https://rubytalk.org/t/2d-array-strategies/66405/1 "2012-10-02T18:49:05Z")

</div>

I'm a relative Ruby Nuby, and am running into some difficulty  
determining how to handle 2D arrays. An extremely common task I am  
using Ruby for is to post-process the output files of large engineering  
codes, and extract scattered information into structured output for  
visualization codes. Frequently this amounts to pulling the same  
information from multiple cases, and producing x vs. y1, y2, ..., yn  
data.

It is easy to do this for a single array case, but the capacity for Ruby  
to handle 2D array data is quite strange (and even awkward) to me.  
Coming from a Fortran 90 type of background, multiple-dimension array  
structures are very simple.

So getting to my question, what is a typical approach for a Ruby user to  
solve a problem of this sort:

> Set up a data structure to store at least one set of x versus y data.

> Parse a file, looking to identify x versus y data for the first case out of an

indeterminant overall number of cases.

> If I find a subsequent case, add on an x versus y1 set of data to the original

data structure

> Repeat until EOF.

Since Ruby only does 1D arrays, is there a trick for an array of arrays?  
Or is this hash territory? How do I do the 2D equivalent of using the  
.push array command to add on more data to the original array? Are  
there are any particular Ruby resources that I need to go read up on and  
go practice in IRB?

Respectfully, TPL

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![7stud2](https://avatars.discourse-cdn.com/v4/letter/7/9de053/32.png) [@7stud2](https://rubytalk.org/u/7stud2)\
**Post date:** [2 October 2012 19:09 UTC](https://rubytalk.org/t/2d-array-strategies/66405/2 "2012-10-02T19:09:36Z")

</div>

array2d = [  
&nbsp;&nbsp;['a', 'b', 'c'],  
&nbsp;&nbsp;['x', 'y', 'z']  
]

puts array2d[1][0]

--output:--  
x

array2d[0] \<\< 'd'  
p array2d

--output:--  
[["a", "b", "c", "d"], ["x", "y", "z"]]

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Ryan\_Davis1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/ryan_davis1/32/1848_2.png) [@Ryan\_Davis1](https://rubytalk.org/u/Ryan_Davis1)\
**Post date:** [2 October 2012 20:14 UTC](https://rubytalk.org/t/2d-array-strategies/66405/3 "2012-10-02T20:14:22Z")

</div>

Unless your dimensions are quite huge, you might want to try out Matrix:

% ri Matrix  
= MMaattrriixx \<\< OObbjjeecctt

(from ruby core)

> **···**
>
> On Oct 2, 2012, at 11:49 , Thomas Luedeke \<lists@ruby-forum.com\> wrote:
> 
> > Since Ruby only does 1D arrays, is there a trick for an array of arrays?  
> > Or is this hash territory? How do I do the 2D equivalent of using the  
> > .push array command to add on more data to the original array? Are  
> > there are any particular Ruby resources that I need to go read up on and  
> > go practice in IRB?
> 
> ------------------------------------------------------------------------------  
> The Matrix class represents a mathematical matrix, and provides methods for  
> creating special-case matrices (zero, identity, diagonal, singular, vector),  
> operating on them arithmetically and algebraically, and determining their  
> mathematical properties (trace, rank, inverse, determinant).
> 
> Note that although matrices should theoretically be rectangular, this is not  
> enforced by the class.
> 
> Also note that the determinant of integer matrices may be incorrectly  
> calculated unless you also require 'mathn'. This may be fixed in the future.
> 
> == MMeetthhoodd CCaattaalloogguuee
> 
> To create a matrix:  
> \* Matrix[\*rows]  
> \* Matrix.(\*rows)  
> \* Matrix.rows(rows, copy = true)  
> \* Matrix.columns(columns)  
> \* Matrix.diagonal(\*values)  
> \* Matrix.scalar(n, value)  
> \* Matrix.scalar(n, value)  
> \* Matrix.identity(n)  
> \* Matrix.unit(n)  
> \* Matrix.I(n)  
> \* Matrix.zero(n)  
> \* Matrix.row\_vector(row)  
> \* Matrix.column\_vector(column)
> 
> To access Matrix elements/columns/rows/submatrices/properties:  
> \* (i, j)  
> \* #row\_size  
> \* #column\_size  
> \* #row(i)  
> \* #column(j)  
> \* #collect  
> \* #map  
> \* #minor(\*param)
> 
> Properties of a matrix:  
> \* #regular?  
> \* #singular?  
> \* #square?
> 
> Matrix arithmetic:  
> \* \*(m)  
> \* +(m)  
> \* -(m)  
> \* #/(m)  
> \* #inverse  
> \* #inv  
> \* \*\*
> 
> Matrix functions:  
> \* #determinant  
> \* #det  
> \* #rank  
> \* #trace  
> \* #tr  
> \* #transpose  
> \* #t
> 
> Conversion to other data types:  
> \* #coerce(other)  
> \* #row\_vectors  
> \* #column\_vectors  
> \* #to\_a
> 
> String representations:  
> \* #to\_s  
> \* #inspect  
> ------------------------------------------------------------------------------  
> = CCllaassss mmeetthhooddss::
> 
> &nbsp;&nbsp;I  
> &nbsp;&nbsp;  
> &nbsp;&nbsp;column\_vector  
> &nbsp;&nbsp;columns  
> &nbsp;&nbsp;diagonal  
> &nbsp;&nbsp;identity  
> &nbsp;&nbsp;new  
> &nbsp;&nbsp;row\_vector  
> &nbsp;&nbsp;rows  
> &nbsp;&nbsp;scalar  
> &nbsp;&nbsp;unit  
> &nbsp;&nbsp;zero
> 
> = IInnssttaannccee mmeetthhooddss::
> 
> &nbsp;&nbsp;\*  
> &nbsp;&nbsp;\*\*  
> &nbsp;&nbsp;+  
> &nbsp;&nbsp;-  
> &nbsp;&nbsp;/  
> &nbsp;&nbsp;==  
> &nbsp;&nbsp;  
> &nbsp;&nbsp;clone  
> &nbsp;&nbsp;coerce  
> &nbsp;&nbsp;collect  
> &nbsp;&nbsp;column  
> &nbsp;&nbsp;column\_size  
> &nbsp;&nbsp;column\_vectors  
> &nbsp;&nbsp;compare\_by\_row\_vectors  
> &nbsp;&nbsp;det  
> &nbsp;&nbsp;determinant  
> &nbsp;&nbsp;eql?  
> &nbsp;&nbsp;hash  
> &nbsp;&nbsp;init\_rows  
> &nbsp;&nbsp;inspect  
> &nbsp;&nbsp;inv  
> &nbsp;&nbsp;inverse  
> &nbsp;&nbsp;inverse\_from  
> &nbsp;&nbsp;map  
> &nbsp;&nbsp;minor  
> &nbsp;&nbsp;rank  
> &nbsp;&nbsp;regular?  
> &nbsp;&nbsp;row  
> &nbsp;&nbsp;row\_size  
> &nbsp;&nbsp;row\_vectors  
> &nbsp;&nbsp;singular?  
> &nbsp;&nbsp;square?  
> &nbsp;&nbsp;t  
> &nbsp;&nbsp;to\_a  
> &nbsp;&nbsp;to\_s  
> &nbsp;&nbsp;tr  
> &nbsp;&nbsp;trace  
> &nbsp;&nbsp;transpose

---

<div class="post-metadata">

**Author:** ![7stud2](https://avatars.discourse-cdn.com/v4/letter/7/9de053/32.png) [@7stud2](https://rubytalk.org/u/7stud2)\
**Post date:** [2 October 2012 19:24 UTC](https://rubytalk.org/t/2d-array-strategies/66405/4 "2012-10-02T19:24:50Z")

</div>

7stud -- wrote in post #1078413:

> array2d = [  
> &nbsp;&nbsp;['a', 'b', 'c'],  
> &nbsp;&nbsp;['x', 'y', 'z']  
> ]
> 
> puts array2d[1][0]
> 
> --output:--  
> x
> 
> array2d[0] \<\< 'd'  
> p array2d
> 
> --output:--  
> [["a", "b", "c", "d"], ["x", "y", "z"]]
> 
> > Set up a data structure to store at least one set of x versus y data.
> 
> xy\_data = [  
> &nbsp;&nbsp;[1, 2],  
> &nbsp;&nbsp;[3, 4]  
> ]
> 
> > Parse a file, looking to identify x versus y data for the  
> > first case out of an indeterminant overall number of cases.
> 
> That is just pure gibberish. Getting ready to debate Obama?
> 
> > If I find a subsequent case, add on an x versus y1 set of  
> > data to the original data structure
> 
> More gibberish. How about a concrete example?

OK. Try this as a file content:

============================================================= (BOF)  
Gibberish

Gibberish

Gibberish

Case #1 Output  
1 2  
2 4  
3 6  
4 8  
5 10  
6 12  
7 14

Gibberish

Gibberish

Gibberish

Case #2 Output  
1 1  
2 3  
3 5  
4 7  
5 9  
6 11  
7 13

Gibberish

Gibberish

Gibberish  
========================================================== (EOF)

I parse the file and identify the data associated with the first case,  
and load it up into an array. I then parse through gibberish until I  
identify the data associated with the second case. Now what?

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![7stud2](https://avatars.discourse-cdn.com/v4/letter/7/9de053/32.png) [@7stud2](https://rubytalk.org/u/7stud2)\
**Post date:** [2 October 2012 19:45 UTC](https://rubytalk.org/t/2d-array-strategies/66405/5 "2012-10-02T19:45:27Z")

</div>

Thomas Luedeke wrote in post #1078414:

> I parse the file and identify the data associated with the first case,  
> and load it up into an array. I then parse through gibberish until I  
> identify the data associated with the second case. Now what?

Hi,

It really depends on the actual data. If you really want the most  
efficient, you can try the NArray. (Or, if you are using Windows, you  
can try to use RTensor ([http://tensor.heliohost.org](http://tensor.heliohost.org)) which I wrote a  
while ago.) At least the arrays will be 2D. If all the cases have the  
same amount of data, then you can make a single 3D array.

If on the other hand the amount of data is not much, then you can use a  
combination of Ruby arrays and/or hashes.

Regards,

Bill

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).
