# 2dim array/matrix

**URL:** <https://www.ruby-forum.com/t/2dim-array-matrix/48995>\
**Category:** Ruby\
**Created:** [November 28, 2005, 1:23pm UTC](https://www.ruby-forum.com/t/2dim-array-matrix/48995 "2005-11-28T13:23:38Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![wollez](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/58f4c7/32.png) [@wollez](https://www.ruby-forum.com/u/wollez)\
**Post date:** [November 28, 2005, 1:23pm UTC](https://www.ruby-forum.com/t/2dim-array-matrix/48995/1 "2005-11-28T13:23:38Z")

</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:** ![wollez](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/58f4c7/32.png) [@wollez](https://www.ruby-forum.com/u/wollez)\
**Post date:** [November 28, 2005, 1:56pm UTC](https://www.ruby-forum.com/t/2dim-array-matrix/48995/2 "2005-11-28T13:56:03Z")

</div>

Wolfgang wrote:

> 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|  
matrix.each\_with\_index do |yvalues, xidx|  
yvalues.each\_with\_index do |val, yidx|  
io.print xidx,yidx,val,“\n”  
end  
end  
end

etc.

HTH

Kind regards

```
robert

```

---

<div class="post-metadata">

**Author:** ![wollez](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/58f4c7/32.png) [@wollez](https://www.ruby-forum.com/u/wollez)\
**Post date:** [November 28, 2005, 2:04pm UTC](https://www.ruby-forum.com/t/2dim-array-matrix/48995/3 "2005-11-28T14:04:12Z")

</div>

Wolfgang wrote:

> 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 [RDoc Documentation](http://www.ruby-doc.org/stdlib/).

---

<div class="post-metadata">

**Author:** ![wollez](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/58f4c7/32.png) [@wollez](https://www.ruby-forum.com/u/wollez)\
**Post date:** [November 28, 2005, 2:52pm UTC](https://www.ruby-forum.com/t/2dim-array-matrix/48995/4 "2005-11-28T14:52:44Z")

</div>

On 28/11/05, Wolfgang [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> this works but when I try:  
> m.=123  
> puts m  
> I only get an error.
> 
> Wolfgang

Try these:

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

brian

–  
[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:** ![wollez](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/58f4c7/32.png) [@wollez](https://www.ruby-forum.com/u/wollez)\
**Post date:** [November 28, 2005, 2:52pm UTC](https://www.ruby-forum.com/t/2dim-array-matrix/48995/5 "2005-11-28T14:52:45Z")

</div>

Wolfgang wrote:

> this works but when I try:  
> m.=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

```
robert
```

---

<div class="post-metadata">

**Author:** ![wollez](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/58f4c7/32.png) [@wollez](https://www.ruby-forum.com/u/wollez)\
**Post date:** [November 28, 2005, 3:04pm UTC](https://www.ruby-forum.com/t/2dim-array-matrix/48995/6 "2005-11-28T15:04:52Z")

</div>

> > > > > “R” == Robert K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

R\> 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:** ![wollez](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/58f4c7/32.png) [@wollez](https://www.ruby-forum.com/u/wollez)\
**Post date:** [November 28, 2005, 4:05pm UTC](https://www.ruby-forum.com/t/2dim-array-matrix/48995/7 "2005-11-28T16:05:38Z")

</div>

ts wrote:

> > > > > > “R” == Robert K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:
> 
> > 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” 🙂

```
robert

```

---

<div class="post-metadata">

**Author:** ![wollez](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/58f4c7/32.png) [@wollez](https://www.ruby-forum.com/u/wollez)\
**Post date:** [November 28, 2005, 2:44pm UTC](https://www.ruby-forum.com/t/2dim-array-matrix/48995/8 "2005-11-28T14:44:39Z")

</div>

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.  
this works but when I try:  
m.=123  
puts m  
I only get an error.

Wolfgang
