This task is about arrays.

For hashes or associative arrays, please see Creating an Associative Array.

For a definition and in-depth discussion of what an array is, see Array.



Task

Show basic array syntax in your language.

Basically, create an array, assign a value to it, and retrieve an element   (if available, show both fixed-length arrays and
dynamic arrays, pushing a value into it).

Please discuss at Village Pump:   Arrays.

Please merge code in from these obsolete tasks:

   Creating an Array
   Assigning Values to an Array
   Retrieving an Element of an Array


Related tasks

   Collections
   Creating an Associative Array
   Two-dimensional array (runtime)


 
// 1-D (row- or column-vectors)
// Static:
// row-vector
x = [1:3];  
x = zeros(1,3); x[1]=1; x[2]=2; x[3]=3;
// column-vector
x = [1:3]';  // or
x = [1;2;3]; // or
x = zeros(3,1); x[1]=1; x[2]=2; x[3]=3;
// Dynamic:
x = [];           // create an empty array
x = [x; 1, 2];    // add a row to 'x' containing [1, 2], or
x = [x, [1; 2]];  // add a column to 'x' containing [1; 2]
 
// 2-D array
// Static:
x = zeros(3,5);        // create an zero-filed matrix of size 3x5
x[1;1] = 1;            // set the x(1,1) element to 1
x[2;]  = [1,2,3,4,5];  // set the second row x(2,) to a row vector
x[3;4:5] = [2,3];      // set x(3,4) to 2 and x(3,5) to 3
// Dynamic
x = [1:5];               // create an row-vector x(1,1)=1, x(1,2)=2, ... x(1,5)=5
x = [x; 2, 3, 4, 6, 7];  // add to 'x' a row.
 
// Accessing an element of arrays:
// to retrieve/print element of matrix 'x' just put this in a single line in the script
i=1;
j=2;
x[i;j]
 
 
 