DEV Community

Discussion on: ML Fundamentals in Javascript

Collapse
 
swatirajan7 profile image
Swati Rajan

Great article! I had no idea about math.eval() Can we apply them to matrices too?

Collapse
 
mrinasugosh profile image
Mrinalini Sugosh (Mrina)

@swatirajan7 Glad you found it useful! Yes it can...For instance, it can be used to extract a subset of a Matrix by range indices. For example, this snippet returns the first and second column of Matrix A (indices start with 1) with all their rows as two vectors in a new matrix.

let matAsub = math.eval('matA[:, 1:2]', { matA });
Enter fullscreen mode Exit fullscreen mode
Collapse
 
mrinasugosh profile image
Mrinalini Sugosh (Mrina)

Actually, I would even propose taking it a step further and assign columns in a matrix to a new vector:

math.eval(`matA[:, 1] = vectorB`, { matA, vectorB });
Enter fullscreen mode Exit fullscreen mode