Class: Matlab::Matrix

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/matlab/matrix.rb,
lib/matlab/driver/native/conversions.rb

Overview

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

require 'matlab'

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

Direct Known Subclasses

CellMatrix, StructMatrix

Instance Attribute Summary collapse

Class Method 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/matlab/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/matlab/matrix.rb', line 17

def cells
  @cells
end

#mObject (readonly)

Returns the value of attribute m.



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

def m
  @m
end

#nObject (readonly)

Returns the value of attribute n.



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

def n
  @n
end

Class Method Details

.from_matlab(matrix) ⇒ Object

Creates a Matlab::Matrix or Ruby Array from a MATLAB numeric matrix



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/matlab/driver/native/conversions.rb', line 112

def self.from_matlab(matrix)
  m = Matlab::Driver::Native::API.mxGetM(matrix)
  n = Matlab::Driver::Native::API.mxGetN(matrix)
  
  matlab_matrix = self.new(m, n)
  double_array = Matlab::Driver::Native::API::DoubleArray.frompointer(Matlab::Driver::Native::API.mxGetPr(matrix))
  
  index = 0
  n.times do |column_index|
    m.times do |row_index|
      matlab_matrix[row_index, column_index] = (Matlab::Driver::Native::API.mxIsNaN(double_array[index]) ? nil : double_array[index])
      index += 1
    end
  end
  
  if m == 1 || n == 1
    matlab_matrix.cells.flatten
  else
    matlab_matrix
  end
end

Instance Method Details

#==(other) ⇒ Object

Enables comparisons of matrices to each other



36
37
38
# File 'lib/matlab/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/matlab/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/matlab/matrix.rb', line 31

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

#to_matlabObject

Converts the matrix into a MATLAB numeric matrix



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/matlab/driver/native/conversions.rb', line 95

def to_matlab
  matrix = Matlab::Driver::Native::API.mxCreateDoubleMatrix(m, n, Matlab::Driver::Native::API::MxREAL)
  double_array = Matlab::Driver::Native::API::DoubleArray.new(m * n)
  
  index = 0
  n.times do |column_index|
    m.times do |row_index|
      double_array[index] = (@cells[row_index][column_index] ? @cells[row_index][column_index].to_f : nil.to_matlab)
      index += 1
    end
  end
  
  Matlab::Driver::Native::API.mxSetPr(matrix, double_array)
  matrix
end