We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1
● Operators: +, -, /, *
● Use semicolon (;) to execute multiple commands in the command window.
● Workspace displays saved variables. ● Up arrow key lets you navigate between previous commands. ● MATLAB variables need to start with a letter and contain only letters, numbers, and underscores (_). ● Use brackets wherever necessary in the command window. ● To save all the variables in the workspace to a MAT-file named filename.mat, use: save filename ● To load variables from a MAT-file, use: load filename ● Use clear to clear the workspace, and clc to clear the command window. ● Built-in functions: pi, sqrt, sin, max, etc. ● Use format long or format short to change the number of displayed decimal places. ● All MATLAB variables are arrays. ● A single number is called a scalar, and is represented by a 1-by-1 array. ● When you separate numbers by using spaces, MATLAB combines the numbers into a row vector, which is an array with one row and multiple columns. When you separate numbers by using semicolons, MATLAB creates a column vector. Use a combination of both to create a matrix. ● For creating evenly spaced vectors is to use the colon (:) and specify only the start and end values. ● For using the linspace function: linspace(first, last, number_of_elements) ● If you need a linearly spaced column vector, the transpose operator (') converts a row vector to a column vector. ● To create a variable named x that is a 5-by-5 matrix of random numbers, use: x = rand(5) ● Similarly, use zeros or ones to create matrices with zeros and ones. ● To determine the size of an existing matrix, use size. ● Use one index to access elements in a vector: x(3) extracts the third element of vector x. ● Change values by assigning new ones to specific indices: x(3) = 1 changes the third element to 1. ● For matrices, use two indices (row, column). ● Extract ranges using a colon: A(:,3) extracts all rows of the third column. ● Use end to extract the last element of a row or column. Use end-1 to extract the second-last. ● Use the colon operator to specify a range of values, like: x = A(1:3,:) ● The .* operator performs element-wise multiplication by multiplying the corresponding elements of two equally sized arrays. ● Plot two vectors of the same length against each other using plot(x,y) ● Specify the color, line style, and marker of a plot by using different symbols in double quotes as another input to the plot function, like: plot(x,y,"r--o"). Use LineWidth within plot to change the line thickness. ● Plot one line on top of another in the same axes by using the hold on command. Use hold off to return to default. ● Add labels to plots using title, ylabel, xlabel, etc ● To extract a variable from a table, you can use dot notation, like: var = table.VariableName ● Relational operators, such as >, <, ==, and ~= perform comparisons between two values. The outcome of a comparison for equality or inequality is either 1 (true) or 0 (false). ● Combine logical comparisons by using the logical operators AND (&) and OR (|). ● Use an if statement to execute code only if a condition is true, such as calculating the square root if x is positive. For multiple conditions, use if-else blocks. ● To repeat commands use a for loop, which iterates over a range of values.