Once you bring values into MATLAB and store them in variables, you can perform several types of actions with them. Learn how the symbols you use to indicate those actions, called operators, let you do arithmetic, compare values, and evaluate logical expressions.
- [Instructor] Once you bring values into MATLAB and store them in variables, or perhaps just type them in, you can perform several types of actions with them. The symbols you use to indicate those actions, called operators, let you do arithmetic calculations, compare values, and evaluate logical expressions. In this movie, I'll review the most commonly used MATLAB operators and show you how to use them in a script. I'll start with a presentation and then move to the command window. We'll start with mathematical operators, which, again, let you do arithmetic calculations.
Most of them are common and you've likely seen them before. We have the plus sign for addition, the minus sign for subtraction, the asterisk for multiplication, and the forward slash for division. So, for example, seven divided by 14 is 0.5 or one half. An operator you might not have seen before is the divides into operator, and this is a backslash. So, for example, seven goes into 14 twice.
So, seven forward slash 14 is seven divided by 14, that's one half or 0.5, however, seven and then with a backslash 14, meaning seven divides into 14, that would be equal to two. So, you probably won't use the backslash that much for divides into, but it's there if you need it. You can also use the minus sign for a negation, such as minus 19, or if you have a calculation in parentheses, for example, minus and then in parentheses eight times nine would give you minus 72.
You'll often see the minus sign in front of an entire calculation, for example, as the first character. Or you could also see it as regular subtraction, which indicates that the value will be negative anyway. And you can also use the caret for exponents. And as always, parentheses are used to group operations and they override the normal order of operations within MATLAB. So, what is that normal order of operations? Well, in MATLAB, it's what you've probably come to expect in other programs such as Excel.
You start with the exponentiation, that's the caret, and then you have the minus sign for negation, then multiplication or division and divides into, and then finally, plus and minus, so addition and subtraction. If you're working in English or most other Western languages, then you go left to right if the calculations are at the same level. On the other hand, if you want to change the order of operations, then you can use parentheses, and I'll give you an example of that when I get into the command window in MATLAB itself.
Next are the comparison operators, which let you compare two or more values. Two equal signs together checks for equality. You use a single equal sign to assign a value to a variable, but you need to use two equal signs to check to see whether two values are equal. So, one equal sign for assignment, two equal signs to check for equality. And then you're probably familiar with the greater than sign, a right-pointing caret, and then less than, greater than or equal to, less than or equal to, and is not equal to or doesn't equal is a little bit different.
In MATLAB, it is a tilde followed by an equal sign. The tilde is not, so in this case, it literally means not equal to. And finally, we have the logical operators. So, we have OR, AND, plus NOT. Two vertical bars, or pipe symbols if you use a lot of Unix or Linux, is OR. That means at least one condition is true. Only if everything is false does OR return a false or zero value. Two ampersands is AND, that means all conditions are true.
And as I mentioned before, the tilde is the NOT sign, so that means that the condition is not true. Alright, that's a lot of overview. Let me switch over to MATLAB and show you how these work in practice.
- Defining variables and contains
- Exploring operators
- Summarizing with built-in functions
- Generating random numbers
- Defining vectors and matrices
- Accepting input in scripts
- Writing and reading data from external files
- Creating custom functions
- Using conditional logic
- Repeating operations with loops
- Working with text strings
- Plotting data and function output
- Formatting, saving, and printing plots
- Using statistical and matrix functions