Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

110
Views
Operations on high dimension arrays in javascript

Let's say I have an array a of shape (3, 3, 3) and a vector b of size 3. I want to iterate on the second axis of the first array, and multiply the first array by the first term of b, the second array by the second term of b etc.

In python I would simply write something like

a = np.ones([3, 3, 3])
b = np.array([1, 2, 3])
for i in range(3):
  a[:, i, :] *= b[i]

(I could even use vectorized functions to do this).

I'm strugling to do it in a clean way in js, as I don't want to loop on every axis (I have matrices with 6 axes, it would be very unefficient.

Currently I use NumJs, and I do it this way:

var a = nj.ones([3, 3, 3]);
let b = [1, 2, 3];
for (let i = 0; i < 3; i++) {
  for (let j = 0; j < 3; j++) {
    a.slice([i, i+1], [j, j+1]).multiply(b[j], false)
  }
}

I have to loop on 2 axes and it doesn't look very efficient.

Are there better ways to do this kind of operations in js? (I would also like to do other operations like to sum an array on some axes only) Thanks!

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!