# 2dim array/matrix

**URL:** <https://rubytalk.org/t/2dim-array-matrix/22818>\
**Category:** ruby-talk\
**Created:** [28 November 2005 12:22 UTC](https://rubytalk.org/t/2dim-array-matrix/22818 "2005-11-28T12:22:29Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Wolfgang](https://avatars.discourse-cdn.com/v4/letter/w/54ee81/32.png) [@Wolfgang](https://rubytalk.org/u/Wolfgang)\
**Post date:** [28 November 2005 12:22 UTC](https://rubytalk.org/t/2dim-array-matrix/22818/1 "2005-11-28T12:22:29Z")

</div>

hello,

I'm quite new to programming as well to ruby. I want to store data in a 2 dimensional array. How can I do this?

A more detailed describtion of what I want to do:  
I have data from a digital elevation model. This is stored in a log txt file. I want to read it in and store it in an array or matrix. Because there are some error/gaps in the data I want to check for these values and calculate an average from the surrounding values. At the end all values should get written to an other txt file as x, y, z triples. The main problem for me to start is the 2dimensional storage. I know the needed size of the 2dim array/matrix (x-dim,y-dim). For getting the data in I was thinking of something like: dem[xi][yi]=value .

Kind regards

Wolfgang

---

<div class="post-metadata">

**Author:** ![Robert](https://avatars.discourse-cdn.com/v4/letter/r/e95f7d/32.png) [@Robert](https://rubytalk.org/u/Robert)\
**Post date:** [28 November 2005 12:52 UTC](https://rubytalk.org/t/2dim-array-matrix/22818/2 "2005-11-28T12:52:29Z")

</div>

Wolfgang wrote:

> hello,
> 
> I'm quite new to programming as well to ruby. I want to store data in  
> a 2 dimensional array. How can I do this?
> 
> A more detailed describtion of what I want to do:  
> I have data from a digital elevation model. This is stored in a log  
> txt file. I want to read it in and store it in an array or matrix.  
> Because there are some error/gaps in the data I want to check for  
> these values and calculate an average from the surrounding values. At  
> the end all values should get written to an other txt file as x, y, z  
> triples. The main problem for me to start is the 2dimensional  
> storage. I know the needed size of the 2dim array/matrix  
> (x-dim,y-dim). For getting the data in I was thinking of something  
> like: dem[xi][yi]=value .

The easiest to achieve that is

matrix = Array.new(dim\_x) { Array.new(dim\_y) }

There are also some matrix classes on the RAA  
[http://raa.ruby-lang.org/search.rhtml?search=matrix](http://raa.ruby-lang.org/search.rhtml?search=matrix)

For storage you can use CSV implementations or cook your own:

File.open("matrix.csv", "w") do |io|  
&nbsp;&nbsp;matrix.each\_with\_index do |yvalues, xidx|  
&nbsp;&nbsp;&nbsp;&nbsp;yvalues.each\_with\_index do |val, yidx|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;io.print xidx,yidx,val,"\n"  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;end  
end

etc.

HTH

Kind regards

&nbsp;&nbsp;&nbsp;&nbsp;robert

---

<div class="post-metadata">

**Author:** ![Tim\_Hunter](https://avatars.discourse-cdn.com/v4/letter/t/7feea3/32.png) [@Tim\_Hunter](https://rubytalk.org/u/Tim_Hunter)\
**Post date:** [28 November 2005 13:02 UTC](https://rubytalk.org/t/2dim-array-matrix/22818/3 "2005-11-28T13:02:58Z")

</div>

Wolfgang wrote:

> hello,
> 
> I'm quite new to programming as well to ruby. I want to store data in a 2 dimensional array. How can I do this?
> 
> A more detailed describtion of what I want to do:  
> I have data from a digital elevation model. This is stored in a log txt file. I want to read it in and store it in an array or matrix. Because there are some error/gaps in the data I want to check for these values and calculate an average from the surrounding values. At the end all values should get written to an other txt file as x, y, z triples. The main problem for me to start is the 2dimensional storage. I know the needed size of the 2dim array/matrix (x-dim,y-dim). For getting the data in I was thinking of something like: dem[xi][yi]=value .
> 
> Kind regards
> 
> Wolfgang

Ruby has a Matrix class in the standard library. Check out "matrix" in the [http://www.ruby-doc.org/stdlib/\](http://www.ruby-doc.org/stdlib/%5C).

---

<div class="post-metadata">

**Author:** ![Wolfgang](https://avatars.discourse-cdn.com/v4/letter/w/54ee81/32.png) [@Wolfgang](https://rubytalk.org/u/Wolfgang)\
**Post date:** [28 November 2005 13:42 UTC](https://rubytalk.org/t/2dim-array-matrix/22818/4 "2005-11-28T13:42:29Z")

</div>

Thanks for the answers,

I think I will use for the beginning this solution:  
&nbsp;&nbsp;mx = Array.new(dim\_x) { Array.new(dim\_y) }

for the matrix class:  
I don't understand how I can set in a matrix single values:  
&nbsp;&nbsp;require "matrix"  
&nbsp;&nbsp;m=Matrix[[25, 93], [-1, 66] ]  
&nbsp;&nbsp;puts m.[](1,1)  
this works but when I try:  
&nbsp;&nbsp;m.[](1,1)=123  
&nbsp;&nbsp;puts m  
I only get an error.

Wolfgang

---

<div class="post-metadata">

**Author:** ![Brian\_Schroder1](https://avatars.discourse-cdn.com/v4/letter/b/2bfe46/32.png) [@Brian\_Schroder1](https://rubytalk.org/u/Brian_Schroder1)\
**Post date:** [28 November 2005 13:49 UTC](https://rubytalk.org/t/2dim-array-matrix/22818/5 "2005-11-28T13:49:59Z")

</div>

Try these:

puts m[1,1]  
m[1,1] = 123  
pits m[1,1]

brian

> **···**
>
> On 28/11/05, Wolfgang \<wollez@gmx.net\> wrote:
> 
> > Thanks for the answers,
> > 
> > I think I will use for the beginning this solution:  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mx = Array.new(dim\_x) { Array.new(dim\_y) }
> > 
> > for the matrix class:  
> > I don't understand how I can set in a matrix single values:  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;require "matrix"  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;m=Matrix[[25, 93], [-1, 66] ]  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts m.(1,1)  
> > this works but when I try:  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;m.(1,1)=123  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts m  
> > I only get an error.
> > 
> > Wolfgang
> 
> --  
> [http://ruby.brian-schroeder.de/](http://ruby.brian-schroeder.de/)
> 
> Stringed instrument chords: [http://chordlist.brian-schroeder.de/](http://chordlist.brian-schroeder.de/)

---

<div class="post-metadata">

**Author:** ![Robert](https://avatars.discourse-cdn.com/v4/letter/r/e95f7d/32.png) [@Robert](https://rubytalk.org/u/Robert)\
**Post date:** [28 November 2005 13:52 UTC](https://rubytalk.org/t/2dim-array-matrix/22818/6 "2005-11-28T13:52:29Z")

</div>

Wolfgang wrote:

> Thanks for the answers,
> 
> I think I will use for the beginning this solution:  
> mx = Array.new(dim\_x) { Array.new(dim\_y) }
> 
> for the matrix class:  
> I don't understand how I can set in a matrix single values:  
> require "matrix"  
> m=Matrix[[25, 93], [-1, 66] ]  
> puts m.(1,1)  
> this works but when I try:  
> m.(1,1)=123  
> puts m  
> I only get an error.

You probably misread the documentation. Did you try

m[1,1]  
m[1,1] = 123

?

Kind regards

&nbsp;&nbsp;&nbsp;&nbsp;robert

---

<div class="post-metadata">

**Author:** ![ts1](https://avatars.discourse-cdn.com/v4/letter/t/a8b319/32.png) [@ts1](https://rubytalk.org/u/ts1)\
**Post date:** [28 November 2005 14:00 UTC](https://rubytalk.org/t/2dim-array-matrix/22818/7 "2005-11-28T14:00:46Z")

</div>

> m[1,1] = 123

Try it 🙂

moulon% ruby -rmatrix -e 'm = Matrix[[25, 93], [-1, 66]]; m[1, 1] = 123'  
-e:1: undefined method `=' for Matrix[[25, 93], [-1, 66]]:Matrix (NoMethodError)  
moulon%

Guy Decoux

---

<div class="post-metadata">

**Author:** ![Robert](https://avatars.discourse-cdn.com/v4/letter/r/e95f7d/32.png) [@Robert](https://rubytalk.org/u/Robert)\
**Post date:** [28 November 2005 15:02 UTC](https://rubytalk.org/t/2dim-array-matrix/22818/8 "2005-11-28T15:02:36Z")

</div>

ts wrote:

> > m[1,1] = 123
> 
> Try it 🙂
> 
> moulon% ruby -rmatrix -e 'm = Matrix[[25, 93], [-1, 66]]; m[1, 1] =  
> 123' -e:1: undefined method `=' for Matrix[[25, 93], [-1,  
> 66]]:Matrix (NoMethodError) moulon%

"cool" 🙂

&nbsp;&nbsp;&nbsp;&nbsp;robert
