F
F
Flagname2020-04-14 19:31:29
JavaScript
Flagname, 2020-04-14 19:31:29

How to fill a large matrix with numbers?

There is a matrix 12x31.

I declare it like this

let matrix = [
    [],
    [],
    [],
    [],
    [],
    [],
    [],
    [],
    [],
    [],
    [],
    []
];

Is it possible somehow easier?

And how in this case to fill in order? How to access each row of a matrix? 12 cycles?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
H
hzzzzl, 2020-04-14
@hzzzzl

[...Array(12)].map(row => [...Array(31)])
fill with numbers - how, from 0 to 12*31?

[...Array(12)].map((row, x) => 
  [...Array(31)].map((col, y) => x * 31 + y)
)

S
Sergey Sokolov, 2020-04-14
@sergiks

Easier: npm install ml-matrix Documentation

import { Matrix } from 'ml-matrix';

const matrix = new Matrix(12, 31);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question