How to Declare a Two-Dimensional Array in Ruby

How to declare an empty 2-dimensional array in Ruby?

You can do:

width = 2
height = 3
Array.new(height){Array.new(width)} #=> [[nil, nil], [nil, nil], [nil, nil]]

Create two-dimensional arrays and access sub-arrays in Ruby

There are some problems with 2 dimensional Arrays the way you implement them.

a= [[1,2],[3,4]]
a[0][2]= 5 # works
a[2][0]= 6 # error

Hash as Array

I prefer to use Hashes for multi dimensional Arrays

a= Hash.new
a[[1,2]]= 23
a[[5,6]]= 42

This has the advantage, that you don't have to manually create columns or rows. Inserting into hashes is almost O(1), so there is no drawback here, as long as your Hash does not become too big.

You can even set a default value for all not specified elements

a= Hash.new(0)

So now about how to get subarrays

(3..5).to_a.product([2]).collect { |index| a[index] }
[2].product((3..5).to_a).collect { |index| a[index] }

(a..b).to_a runs in O(n). Retrieving an element from an Hash is almost O(1), so the collect runs in almost O(n). There is no way to make it faster than O(n), as copying n elements always is O(n).

Hashes can have problems when they are getting too big. So I would think twice about implementing a multidimensional Array like this, if I knew my amount of data is getting big.

How to declare a two-dimensional array in Ruby

A simple implementation for a sparse 2-dimensional array using nested Hashes,

class SparseArray
attr_reader :hash

def initialize
@hash = {}
end

def [](key)
hash[key] ||= {}
end

def rows
hash.length
end

alias_method :length, :rows
end

Usage:

sparse_array = SparseArray.new
sparse_array[1][2] = 3
sparse_array[1][2] #=> 3

p sparse_array.hash
#=> {1=>{2=>3}}

#
# dimensions
#
sparse_array.length #=> 1
sparse_array.rows #=> 1

sparse_array[0].length #=> 0
sparse_array[1].length #=> 1

Creating and iterating a 2d array in Ruby

irb(main):001:0> a = []
=> []
irb(main):002:0> a1 = [1, 2]
=> [1, 2]
irb(main):003:0> a2 = [3, 4]
=> [3, 4]
irb(main):004:0> a.push a1
=> [[1, 2]]
irb(main):005:0> a.push a2
=> [[1, 2], [3, 4]]
irb(main):006:0> a
=> [[1, 2], [3, 4]]
irb(main):007:0> a[0]
=> [1, 2]
irb(main):008:0> a[0][1]
=> 2

Two Dimensional Array With Ruby

What you're doing is initializing an empty array, new_array, and appending elements to it. You're appending the incremented values to a new array instead of changing the values in the array. Something like this should work:

def dilate(array)
new_array=[]
array.each{|i|new_array<<i.dup}
array.each_with_index do |row, rowIndex|
row.each_with_index do |col, colIndex|
if (array[rowIndex][colIndex] > 0)
new_array[rowIndex][colIndex -1] += 1 if colIndex > 0
new_array[rowIndex -1][colIndex] += 1 if rowIndex > 0
new_array[rowIndex][colIndex +1] += 1 if colIndex < array.first.size-1
new_array[rowIndex +1][colIndex] += 1 if rowIndex < array.size-1
end
end
end
return new_array
end

I'm creating a new array, new_array, that's a copy of array, then incrementing its elements.

By the way, you say in your question that you want to "search for any space that contains a 1", but what this is doing is searching for spaces containing values greater than zero. I'm not sure if that's what you wanted.

Combining two arrays to create a two dimensional array in ruby

Try Array#zip

a.zip(b)
=> [[1,4],[2,5],[3,6]]

Best way to turn an array into a 2D array in Ruby

Use Enumerable#each_slice:

each_slice(n) { ... } → nil
each_slice(n) → an_enumerator
Iterates the given block for each slice of n elements. If no block is given, returns an enumerator.

So you'd say:

s.each_slice(2).to_a

or

s.each_slice(2).entries

How do I assign default values for two-dimensional array?

Just supply a second argument with the value:

array = Array.new(10) { Array.new(10, 4) }

Here, the default value is 4 and thus, a 10*10 2D array is created with default value of 4.



Related Topics



Leave a reply



Submit