From the course: MATLAB Essential Training
Define ranges of values - MATLAB Tutorial
From the course: MATLAB Essential Training
Define ranges of values
- [Presenter] Many of the calculations you perform in MATLAB will use single values. However, there will be times when you want to define a range of values such as the odd numbers from 11 to 19 or the powers of 10. In this movie, I will show you how to create those ranges in your script. I have opened a new workspace and command window and MATLAB, and let's say in the command window that I want to assign a range of values from three to seven incremented by one to the variable A. To do that, I'll type a three, which is a starting value, then a colon and a seven, which is the final value. And when I press enter, you see I get three, four, five, six, and seven. If you don't put in a step value, MATLAB assumes that it should be one. If you do want to add a step value, you can add that to your range assignment. That's all type B equal. And then I'll start again at the number three, followed by a colon, and here the step value will be three followed by another colon, and then the maximum of 15. And when I press enter, I get three, six, nine, 12, and 15, three incremented by three until we reach the maximum value of 15. If the maximum value that you put into your assignment, that is your range definition can't be reached, then MATLAB will go as far as it can. So if I type C equals three, colon three, colon 14, then the last number that I see should be 12, and that's because 15 is larger than 14 and MATLAB therefore stops at 12. The step value can also be negative. So if I have a variable name D type equal, I'd start at 20, then a colon and minus four, colon four. So now, we'll go from 20 to four in steps of minus four, so I get 20, 16, 12, eight, and four. One other way to define a linear space of values is to use the lens space keyword. So let's say that I have a variable name lins, LINS then equal, and I want to go from seven to 35 and have five values. So for that, I would have lin space, which is the key word to use, the left parentheses, and then the first value will be seven, then the last value would be 35, and I want to have five values in that range. So, I'll type a five and a right parentheses and enter, and I get seven, 14, 21, 28, and 35. You can do the same thing with logarithmic values, which go up by powers of 10. So if I have a variable named L-O-G-S-P, and then equal, I would use the log space keyword. Then the left parentheses, and here, I will start at one, then a comma, and then five and five. So this command will give me the first five powers of 10. So when I press enter, I get 10, 100, 1000, 10,000 and 100,000. One final thing to note is that you can concatenate or combine two sequences into a single one using brackets. So I have sequence B, which is three, three, 15, and D, which is 23, four. Again, incremented in steps of minus four. So if I assign those two to the variable named E, then equal, I would use a left square bracket B, which is the first variable, then D, which is the second, and a right square bracket to close out and enter. And you see that MATLAB has combined or concatenated those two ranges into a single variable. Hopefully, these shortcuts will let you create the data that you need more quickly and efficiently.
Contents
-
-
-
Define ranges of values4m 8s
-
(Locked)
Generate random numbers4m 1s
-
(Locked)
Perform scalar and element-wise operations4m 11s
-
(Locked)
Describe matrix multiplication2m 21s
-
(Locked)
Perform matrix multiplication4m 36s
-
(Locked)
Use vectors and matrices in a function4m 54s
-
(Locked)
Transpose and find the inverse of matrices4m 29s
-
-
-
-
-
-
-