The assignment operator (=) is used to create variables. For example, A = 10; // This is a scalar S = "A string"; // Creates a string S = "A string " + "by concatenating two"; // String concatenation C = A; // Assign the value of A to C V = [1, 2, 3]; // A vector with entries separated by commas V = [1:2:10]; // A vector created by the range operator M = [1, 2, 3; 4, 5, 6]; // A matrix of two rows and three columns M = [V, 4, 5, 6]; // A vector by concatenating with another vector M = []; // An empty matrix |
A = 10; B = 20; C = A + B; // Adds the two scalars D = C^3; // Takes the cubic power of C A = [10, 20]; B = [11, 21]; // Creates two vectors C = A + B; // Adds the two vectors element by element. Works for matrices too C = A * 2; // Multiplies each entry of A by 2 A = [1, 2, 3; 4, 5, 6]; B = [1, 2; 3, 4; 5, 6]; // Creates two matrices C = A*B; // Multiplies the above two matrices of compatible dimensions C = A`; // Matrix transpose C = A.^2; // Squares each element of A A = [1, 2; 3, 4]; B = [5, 6; 7, 8]; // Creates two square matrices C = A.*B; // Element by element matrix multiplication C = A.^B; // Each element of A raised to the power of corresponding B element C = Inv(A); // Matrix inverse C = A/B; // Matrix left division. Equivalent to A * Inv(B) C = A\B; // Matrix right division. Equivalent to Inv(A)* B |
A = [1, 2, 3, 4]; C = A(1); // The first entry of A C = A([1:3]) // The first three entries A = [1, 2, 3; 4, 5, 6]; // A matrix C = A(1,1); // The entry at the first row & first column C = A(1,:); // The first row and all columns C = A(1,[1,2]) // The 1st and 2nd column entries in the 1st row C = A([1,2,3]) // The first three entries in column major order |
X = [1, 2, 3, 4]; Y = X.^2; // Two vectors
PlotLine(X,Y); // Plots Y vs. X as a line chart
SetTitle("A line plot"); // Sets the title
SetXLabel("X – axis"); // Sets the x-axis label
SetYLabel("Y – axis"); // Sets the y-axis label
SetLegend("The legend"); // Sets the legend text
PlotScatter(X,Y,"new"); // Plots a new scatter plot Y vs. X
PlotBar(X,Y,"new"); // Plots a new bar plot of Y binned in each entry of X