Logo

Programming-Idioms

Declare and initialize a 3D array x, having dimensions boundaries m, n, p, and containing real numbers.
Implementation
Elixir

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 Elixir 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][p];
const m, n, p = 2, 2, 3
var x [m][n][p]float64
x = [[[0 for k in range(p)] for j in range(n)] for i in range(m)]
import numpy
x = numpy.zeros((m,n,p))
let x = vec![vec![vec![0.0f64; p]; n]; m];
#include <stdlib.h>
double ***x=malloc(m*sizeof(double **));
int i,j;
for(i=0;i<m;i++)
{
	x[i]=malloc(n*sizeof(double *));
	for(j=0;j<n;j++)
	{
		x[i][j]=malloc(p*sizeof(double));
	}
}
auto x = new real[][][](m, n, p);
var x: array [m,n,p] of double;
var x = new List.generate(m, (_) => 
                new List.generate(n, (_) => 
                    new List.filled(p, 0.0), 
                    growable: false), 
                growable: false);
x = Array.new(m) { Array.new(n) { Array.new(p) } }
x = [ [ [ k**(i/j) | k<-[1..p] ] | j<-[1..n] ] | i<-[1..m] ]
func make3D(m, n, p int) [][][]float64 {
	buf := make([]float64, m*n*p)

	x := make([][][]float64, m)
	for i := range x {
		x[i] = make([][]float64, n)
		for j := range x[i] {
			x[i][j] = buf[:p:p]
			buf = buf[p:]
		}
	}
	return x
}
X : array (1 .. M, 1 .. N, 1 .. P) of Float := (others => (others => (others => 1.0)));
$x = array_fill(0, $m, array_fill(0, $n, array_fill(0,$p,0)));
X = array(M, N, P).

-spec array(pos_integer(), pos_integer(), pos_integer()) -> [[[float()]]].
array(M, N, P) -> [array(M, N)  || _ <- lists:seq(1, P)].
array(M, N) -> [array(M) || _ <- lists:seq(1, N)].
array(M) -> [rand:uniform() || _ <- lists:seq(1, M)].
local x = {}
for i=1,m do
   x[i] = {}
   for j=1,n do
      x[i][j] = {}
      for k=1,p do
         x[i][j][k] = 0
      end
   end
end
#include <vector>
std::vector<std::vector<std::vector<double>>> x (m, std::vector<std::vector<double>> (n, std::vector<double> (p)));
my $array3d = [
    [ [ 1, 0, 1 ],
      [ 0, 0, 0 ],
      [ 1, 0, 1 ] ],
    [ [ 0, 0, 0 ],
      [ 0, 2, 0 ],
      [ 0, 0, 0 ] ],
    [ [ 3, 0, 3, ],
      [ 0, 0, 0, ],
      [ 3, 0, 3, ] ]
];
const x = new Array(m).fill(
  new Array(n).fill(
    new Array(p).fill(Math.random())
  )
)
var x = new double[m, n, p];
(defparameter *x*
  (make-array (list m n p)
              :element-type 'double-float
              :initial-element 0.0d0))
  real, dimension(:,:,:), allocatable :: x

  allocate (x(m,n,p))
val x = Array(m, { Array(n, { DoubleArray(p) } ) } )
NSArray *x=@[
  @[
    @[@0.1, @0.2, ... ], // p column values
    ... // n sub-rows
  ],
  ... // m rows
];
let x = [[[0.0f64; P]; N]; M];
func make3D[T any](m, n, p int) [][][]T {
	buf := make([]T, m*n*p)

	x := make([][][]T, m)
	for i := range x {
		x[i] = make([][]T, n)
		for j := range x[i] {
			x[i][j] = buf[:p:p]
			buf = buf[p:]
		}
	}
	return x
}
my @x;
my ($m, $n, $p) = (4,3,2);
my $v = 0;

foreach my $mx (0..$m-1) {
    foreach my $nx (0..$n-1) {
        foreach my $px (0..$p-1) {
            $x[$mx][$nx][$px] = $v++;
        }
    }
}