Class: Octave::Matrix

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/octave/matrix.rb

Overview

The Matrix class is used for storing values that are sent to or received from Octave numeric matrices Usage:

require 'octave'

matrix = Octave::Matrix.new(20, 400)
20.times { |m| 400.times { |n| matrix[m, n] = rand } }

Direct Known Subclasses

CellMatrix, StructMatrix

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(m, n) ⇒ Matrix

Creates a new Matrix with the given dimensions for row and column size



20
21
22
23
# File 'lib/octave/matrix.rb', line 20

def initialize(m, n)
  @m, @n = m, n
  @cells = Array.new(m) { Array.new(n) }
end

Instance Attribute Details

#cellsObject (readonly)

Returns the value of attribute cells.



17
18
19
# File 'lib/octave/matrix.rb', line 17

def cells
  @cells
end

#mObject (readonly)

Rows



15
16
17
# File 'lib/octave/matrix.rb', line 15

def m
  @m
end

#nObject (readonly)

Columns



16
17
18
# File 'lib/octave/matrix.rb', line 16

def n
  @n
end

Instance Method Details

#==(other) ⇒ Object

Enables comparisons of matrices to each other



36
37
38
# File 'lib/octave/matrix.rb', line 36

def ==(other)
  @cells == other.cells
end

#[](m, n) ⇒ Object

Gets the value at the given row and column position



26
27
28
# File 'lib/octave/matrix.rb', line 26

def [](m, n)
  @cells[m][n]
end

#[]=(m, n, value) ⇒ Object

Sets the given value at the row and column position



31
32
33
# File 'lib/octave/matrix.rb', line 31

def []=(m, n, value)
  @cells[m][n] = value
end

#to_aObject

Returns flattened cells



41
42
43
# File 'lib/octave/matrix.rb', line 41

def to_a
  @cells.flatten
end