I'm using the matrix element from the math.js library.
I create a matrix:
let eye = math.matrix([
[1, 0, 0],
[0, 1, 0],
[0, 0, 1]
]);
But when I try to access an element of the matrix, it is always undefined.
console.log(eye[0][0]) -> undefined
console.log(eye[0]) -> undefined
Any suggestions? I have read through the documentation for the math.js library and I see nothing about how to access an individual element.
And I do need to use this library, as I am doing matrix-based operations (matrix multiplication).
If you print out eye
, you get the following object.
{
_mathjs: "DenseMatrix",
_data: [
[ 1, 0, 0 ],
[ 0, 1, 0 ],
[ 0, 0, 1 ]
],
_size: [ 3, 3 ]
}
But that is just the internal structure used to hold information about the matrix, you need to call helper methods as seen in the docs.
If you want to access the data, you can always interrogate the internal matrix
data directly. This is a bad idea, because the fields are all treated as private; internal fields (start with an underscore).
const __print = (x) => console.log(JSON.stringify(x));
let eye = math.matrix([
[1, 0, 0],
[0, 1, 0],
[0, 0, 1]
]);
__print(eye._data[0][0]); // 1
__print(eye._data[0]); // [1, 0, 0]
__print(eye._data[1]); // [0, 1, 0]
__print(eye._data[2]); // [0, 0, 1]
.as-console-wrapper { top: 0; max-height: 100% !important; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/mathjs/8.1.1/math.js"></script>
It is recommended that you use the helper methods which are available in the library.
Note: After fine-tuning my response, I found a similar method of retrieving a row in the Issue #230 on GitHub.
You need to figure out the number of columns in the matrix. After that, you can grab a subset starting at the desired row index with a range of 0 to the number of columns in the row. The result must then be flattened.
const __print = (x) => console.log(JSON.stringify(x));
let eye = math.matrix([
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]);
const __row = (m, r) =>
(([ rows, cols ]) => math.flatten(math.subset(m,
math.index(r, math.range(0, cols))).valueOf()))
(m.size());
const __col = (m, c) =>
(([ rows, cols ]) => math.flatten(math.subset(m,
math.index(math.range(0, rows), c)).valueOf()))
(m.size());
const __cell = (m, r, c) =>
math.subset(m, math.index(r, c));
__print(__cell(eye, 0, 0)); // 1
__print(__row(eye, 0)); // [1, 2, 3]
__print(__row(eye, 1)); // [4, 5, 6]
__print(__row(eye, 2)); // [7, 8, 9]
__print(__col(eye, 0)); // [1, 4, 7]
__print(__col(eye, 1)); // [2, 5, 8]
__print(__col(eye, 2)); // [3, 6, 9]
.as-console-wrapper { top: 0; max-height: 100% !important; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/mathjs/8.1.1/math.js"></script>
Turns out that they are already implemented, but you still need to flatten the result.
const __print = (x) => console.log(JSON.stringify(x));
let eye = math.matrix([
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]);
const __row = (m, r) =>
math.flatten(math.row(m, r).valueOf());
const __col = (m, c) =>
math.flatten(math.column(m, c).valueOf());
const __cell = (m, r, c) =>
math.subset(m, math.index(r, c));
__print(__cell(eye, 0, 0)); // 1
__print(__row(eye, 0)); // [1, 2, 3]
__print(__row(eye, 1)); // [4, 5, 6]
__print(__row(eye, 2)); // [7, 8, 9]
__print(__col(eye, 0)); // [1, 4, 7]
__print(__col(eye, 1)); // [2, 5, 8]
__print(__col(eye, 2)); // [3, 6, 9]
.as-console-wrapper { top: 0; max-height: 100% !important; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/mathjs/8.1.1/math.js"></script>