Join Patrick Royal for an in-depth discussion in this video Everything is a matrix, part of Learning MATLAB.
Let's examine what it means to say that everything in MATLAB is a matrix. One of the key differences between MATLAB and other programming languages like Java or C is that MATLAB is built from the ground up around working with matrices an large data sets. This focus can be confusing and counterintuitive, but it also makes MATLAB one of the most efficient programs to do statistical analysis with once you figure it out. To start with, I'll open up a new script by clicking the New Script button.
This opens a popup window where we can write all of our code. I'm going to start by writing three different types of variable definitions. First, a equals 3. This means that a is interpreted as a scalar with a value of 3. Next, output b equals 3,4,5;1,2,3. This tells MATLAB that b is a matrix. Commas or spaces separate different values on the same row and semicolons or new lines separate different rows.
Finally, c equals 1:3 causes c to be interpreted as a vector. MATLAB automatically expands 1:3 to be 1,2,3. If I save and then run this file, you can see that this is exactly what happens. Now this might seem confusing at first, because there's no way to know whether a given variable was a scalar, a matrix, or a vector. But in reality, it doesn't matter. As far as MATLAB is concerned, a scalar is just one by one matrix and a vector is just 1 by n or n by 1.
This makes things very simple because the exact same types of operations can be used on each of the different variables. To see how this works, align two new lines. First, a times b, since a is a 1 by 1 matrix, it is interpreted as a scalar and multiplied by every value in the matrix b. Next, I will put in c times b transpose. Now, you can't multiply a vector by a matrix, so MATLAB automatically treats c like it's a 1 by 3 matrix instead.
The apostrophe after b tells MATLAB to transpose matrix b. I have to do this in order to make the inner dimensions of b and c match. Now, since we're dealing with matrices, MATLAB treats the asterisk as matrix multiplication, rather than scalar multiplication. Anytime there is no ambiguity about what kind of operation you want. MATLAB interprets your functions in the most appropriate way, which saves a lot of time that you might spend defining and choosing a bunch of different functions to handle matrix multiplication, scalar multiplication, dot products, and so on.
- Installing MATLAB
- Working with MATLAB variables
- Working with matrix and scalar operations
- Creating functions
- Understanding performance considerations
- Building basic plots
- Creating responsive programs
- Editing variables manually
- Working with the Statistics Toolbox