Logo

Programming-Idioms

Declare and initialize a matrix x having m rows and n columns, containing real numbers.
Implementation
Dart

Implementation edit is for fixing errors and enhancing with metadata. Please do not replace the code below with a different implementation.

Instead of changing the code of the snippet, consider creating another Dart implementation.

Be concise.

Be useful.

All contributions dictatorially edited by webmasters to match personal tastes.

Please do not paste any copyright violating material.

Please try to avoid dependencies to third-party libraries and frameworks.

Other implementations
double[][] x = new double[m][n];
const m, n = 3, 4
var x [m][n]float64
var x = [];
for (var i = 0; i < m; i++) {
  x[i] = [];
}
my @array = (
   [ 1.0, 0.0, 0.0 ],
   [ 0.0, 1.0, 0.0 ],
   [ 0.0, 0.0, 1.0 ],
   "first three slots are a 3x3 identity matrix",
   "fourth and fifth slots contain strings"
);
   
let mut x = vec![vec![0.0f64; N]; M];
#include <stdlib.h>
double **x=malloc(m*sizeof(double *));
int i;
for(i=0;i<m;i++)
	x[i]=malloc(n*sizeof(double));
auto x = new double[][](m, n);
var x = new List.generate(m, (_) => new List(n));
var _x: array[_m,_n] of Double;
x = Array.new(m) { Array.new(n) }
x = [ [ j**(1/i) | j <- [1..n] ] | i <- [1..m] ]
func make2D(m, n int) [][]float64 {
	buf := make([]float64, m*n)

	x := make([][]float64, m)
	for i := range x {
		x[i] = buf[:n:n]
		buf = buf[n:]
	}
	return x
}
const int m = 2;
const int n = 3;
double x[m][n];
X = [{R * 1.0, C * 1.0} || R <- lists:seq(1, M), C <- lists:seq(1, N)].
var x = new double[m, n];
X : array (1 .. M, 1 .. N) of Float := (others => (others => 1.0));
$x = array();
$x = array_pad($x, m, 1);
for($i = 0; $i < count($x); $i++){
  $x[$i] = array();
  $x[$i] = array_pad($x[$i],n,1);
}
local x = setmetatable({},{
   __index = function(t1,k1)
      t1[k1] = setmetatable({},{
         __index = function(t2,k2)
            t2[k2] = 0
            return t2[k2]
         end
      })
      return t1[k1]
   end
})
(build-list m (lambda (x)
                (build-list n (lambda (y) 0))))
#include <vector>
std::vector<std::vector<double>> x (m, std::vector<double>(n));
let mut x = [[0.0; N] ; M];
const x = new Array(m).fill(new Array(n).fill(Math.random()));
val x = Array.ofDim[Double](m,n)
(for [i (range 1 (inc m))]
  (for [j (range 1 (inc n))]
    (* i j)))
x = for _ <- 1..m, do: for _ <- 1..n, do: 0.0
(make-array (list m n)
   :element-type 'double-float
   :initial-element 1.0d0)
  real, dimension(m,n) :: x
$m = 3;
$n = 4;

$x = array_map(
    function () use ($m) { return array_fill(0, $m, 0); },
    range(1, $n)
);
val x = Array(m, { DoubleArray(n) })
Dim x(m - 1, n - 1) As Double
x = [[0] * n for _ in range(m)]
NSArray *x=@[
  @[@0.1, @0.2, ... ], // n column values
  ... // m rows
];
x := Matrix	
    rows: m
    columns: n
    element: 1.0
x = {}
for i=1,m do
  x[i] = {}
  for j=1,n do
    x[i][j] = 0
  end
end
func make2D[T any](m, n int) [][]T {
	buf := make([]T, m*n)

	x := make([][]T, m)
	for i := range x {
		x[i] = buf[:n:n]
		buf = buf[n:]
	}
	return x
}