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 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 .
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
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.
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
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.