RosettaCodeData/Task/Arrays/RLaB/arrays.rlab

30 lines
927 B
Plaintext

// 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]