0

Let's say I have a Vector for Variable Y which seems something like this:

VarY = 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0

And I would like to use math.js to convert it into a Vector of this kind: (so later I can actually use other math.js functions such as Multiply)

VarY: [[1], [2], [3], [4], [5], [6], [7], [8], [9], [10], [11], [12], [13], [14], [15], [16], [17], [18], [19], [20], [21], [22], [23], [24], [25], [26], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0]]

That is easily done by hardcoding it using math.matrix([[1],[2],...,[0]])

But since this vector will be variable, I am looking for a way to make it dynamic...

I was thinking in something like:

math.matrix(VarY);

But it does not allow variables that way.

EDIT ---

Extra details here:

I am receiving some data as an input, that is what you will see here as "XOriginal"

for(var i=0; i<26; i++){
VarY.push([XOriginal[i].splice(26,1)]); // @@@@ HERE I SAVE A COPY FOR ALL THE Y RESULTS OF THE ORIGINAL DATA
XOriginal[i].splice(26,1);
}

for ( i=0; i<26; i++) {
  VarY.push([0]);
}

At the end of this, my variable VarY when I use console.log, like this:

console.log('VarYReal: ' + VarY);

In my console, it appears like this:

VarYReal: 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0

Now, since this variable is not storing it as an array or as a matrix, that is the reason why I can not use math.multiply

6
  • 2
    Assuming that you actually have Vary: [1, 2, 3, ...] you can do var asArrays = VarY.map(n => [n]) Commented Dec 30, 2019 at 19:08
  • @ChrisG That is a good answer to the question, so why is it a comment? Commented Dec 30, 2019 at 19:13
  • @ChrisG I am trying that way and it doesn't seem to have any effect, I think it's because it's not in the [] form, I don't know if it has something to do the fact that VarY is a bunch of .push() from an array. Commented Dec 30, 2019 at 19:16
  • @RodrigoZazuetaDonnadieu You'll need to be a little more specific about what format your VarY is in, because what you've written (VarY = 1,2,3,...) isn't valid JavaScript. Including the code (or minimal equivalent) for how VarY is assigned and populated would help, or why exactly it isn't working (what is the result of the math.matrix(...)?) Commented Dec 30, 2019 at 19:20
  • @Klaycon Ok, I will try to add some more details in an answer so it's easier to read. Commented Dec 30, 2019 at 19:24

1 Answer 1

3

math.js has a reshape() function. Once you have created a matrix from the original js array, you can use it to change to the matrix you want:

const math = require('mathjs')
let y = [0, 1, 2,  3, 4]
let m = math.matrix(y)  

console.log(m.size())     // [ 5 ]

math.reshape(m, [m.size()[0], 1])

console.log(m.size())     // [ 5, 1 ]
console.log(m.valueOf())  // [ [ 0 ], [ 1 ], [ 2 ], [ 3 ], [ 4 ] ]
Sign up to request clarification or add additional context in comments.

3 Comments

Im sorry to ask, but... When I try to use math.matrix(VarY) it says Script Error... Does that mean that my VarY is in a format that is not an array? I feel so dumb but i can't find my error.
@RodrigoZazuetaDonnadieu Please edit the question with the content of the error (the fact you simply got an error isn't helpful, but errors are written with messages to tell you what you did wrong!) as well as the declaration of VarY, as it's still unclear whether it's an array or some other data structure.
@Klaycon Finally found my problem, now that I learnt how to use JSON.stringify, i see that the variable VarY actually looks like this: [[[1]],[[2]],[[3]],[[4]],[[5]],[[6]],[[7]],[[8]],[[9]],[[10]],[[11]],[[12]],[[13]],[[14]],[[15]],[[16]],[[17]],[[18]],[[19]],[[20]],[[21]],[[22]],[[23]],[[24]],[[25]],[[26]],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0]] So my problem now is how to convert from Prototype into a normal array...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.