Class: Matlab::Matrix
- Inherits:
-
Object
- Object
- Matlab::Matrix
- Includes:
- Comparable
- Defined in:
- lib/matlab/matrix.rb,
lib/matlab/driver/native/conversions.rb
Overview
Direct Known Subclasses
Instance Attribute Summary collapse
-
#cells ⇒ Object
readonly
Returns the value of attribute cells.
-
#m ⇒ Object
readonly
Returns the value of attribute m.
-
#n ⇒ Object
readonly
Returns the value of attribute n.
Class Method Summary collapse
-
.from_matlab(matrix) ⇒ Object
Creates a Matlab::Matrix or Ruby Array from a MATLAB numeric matrix.
Instance Method Summary collapse
-
#==(other) ⇒ Object
Enables comparisons of matrices to each other.
-
#[](m, n) ⇒ Object
Gets the value at the given row and column position.
-
#[]=(m, n, value) ⇒ Object
Sets the given value at the row and column position.
-
#initialize(m, n) ⇒ Matrix
constructor
Creates a new Matrix with the given dimensions for row and column size.
-
#to_matlab ⇒ Object
Converts the matrix into a MATLAB numeric matrix.
Constructor Details
Instance Attribute Details
#cells ⇒ Object (readonly)
Returns the value of attribute cells.
17 18 19 |
# File 'lib/matlab/matrix.rb', line 17 def cells @cells end |
#m ⇒ Object (readonly)
Returns the value of attribute m.
15 16 17 |
# File 'lib/matlab/matrix.rb', line 15 def m @m end |
#n ⇒ Object (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_matlab ⇒ Object
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 |