admin管理员组

文章数量:1310228

It looks like a too basic job. However, I can't do it.

I added math.js to my HTML code

<script src="js/math.min.js"></script>

I define a matrix in firefox console:

var M = math.matrix([[1,0,0,4],[0,1,0,2],[0,5,1,9],[11,2,3,1]]);

So far everything is good.

M
Object { _data: Array[4], _size: Array[2], _datatype: undefined }

Now, I want to access a single element of the matrix:

M.index(1,2)

And I get an error

TypeError: M.index is not a function

It looks like a too basic job. However, I can't do it.

I added math.js to my HTML code

<script src="js/math.min.js"></script>

I define a matrix in firefox console:

var M = math.matrix([[1,0,0,4],[0,1,0,2],[0,5,1,9],[11,2,3,1]]);

So far everything is good.

M
Object { _data: Array[4], _size: Array[2], _datatype: undefined }

Now, I want to access a single element of the matrix:

M.index(1,2)

And I get an error

TypeError: M.index is not a function

Share Improve this question asked Feb 12, 2016 at 23:15 ar2015ar2015 6,16010 gold badges61 silver badges119 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 6

It looks like you need to use math.index.

M.subset(math.index(1, 2));

But the preferred method, as pointed out by it's author, is using .get.

M.get([1, 2]);

As of the time of this writing, this feature is preferred but documentation is still catching up.

Apart from M.get([1, 2]), you can also do -

var a = M._data;    // a is a multidimensional array
console.log(a[1][2]);

本文标签: javascriptMathjs access a single element in a matrixStack Overflow