At it's heart, MATLAB is all about matrices. Indeed, MATLAB is very good with matrices which is why so many engineers and scientists use it.
Let's create a simple matrix of random numbers. In the command window type:
clc;
clear all;
mymatrix = rand(100,5);
You should see that you now that you have a variable called mymatrix which has 100 rows and 5 columns, each with a random number between 0 and 1. To get the value that is in the fourth row and the third column it would be mymatrix(4,3). MATLAB always uses a matrix format of variable_name(rows, columns). Also note, a matrix can be multi-dimensional. As we all know, objects with more than three dimensions are hard to visualize but MATLAB will keep making extra dimensions as needed.
Matrix operations are easy in MATLAB.
To add a constant to your matrix try:
mymatrix = mymatrix + 100;
You could also try:
mymatrix = mymatrix / 20;
To specify an individual row, you could do something like this:
mymatrix(:,2) = mymatrix(:,2) * 10;
MATLAB is interpreting this as all the data, the : symbol in the 2nd column. So, this operation will only affect the second column.
You can also use [ ] to quickly isolate a part of a matrix. For example, try this:
newmatrix = mymatrix(:,[1 2 5]);
This would create a new variable, newmatrix which has all the data in columns 1, 2, and 5. As you can see, MATLAB and matrices are quite powerful.
A few other useful matrix commands - the transpose which is simply:
mymatrix = mymatrix';
The dimensions of the matrix:
size(mymatrix)
The length of the longest size of the matrix. Note, this returns the length of the longest side which could be any dimension:
length(mymatrix)
All of the mathematical operators work on matrices as well. For example:
mean(mymatrix,1)
Takes the mean of the matrix along the first dimension. You could use mean(mymatrix,2) to take the mean along the second dimension and so on.
More useful matrix operators can be found HERE and HERE. Make sure you play around with matrices some more.
Onto the next tutorial!
Let's create a simple matrix of random numbers. In the command window type:
clc;
clear all;
mymatrix = rand(100,5);
You should see that you now that you have a variable called mymatrix which has 100 rows and 5 columns, each with a random number between 0 and 1. To get the value that is in the fourth row and the third column it would be mymatrix(4,3). MATLAB always uses a matrix format of variable_name(rows, columns). Also note, a matrix can be multi-dimensional. As we all know, objects with more than three dimensions are hard to visualize but MATLAB will keep making extra dimensions as needed.
Matrix operations are easy in MATLAB.
To add a constant to your matrix try:
mymatrix = mymatrix + 100;
You could also try:
mymatrix = mymatrix / 20;
To specify an individual row, you could do something like this:
mymatrix(:,2) = mymatrix(:,2) * 10;
MATLAB is interpreting this as all the data, the : symbol in the 2nd column. So, this operation will only affect the second column.
You can also use [ ] to quickly isolate a part of a matrix. For example, try this:
newmatrix = mymatrix(:,[1 2 5]);
This would create a new variable, newmatrix which has all the data in columns 1, 2, and 5. As you can see, MATLAB and matrices are quite powerful.
A few other useful matrix commands - the transpose which is simply:
mymatrix = mymatrix';
The dimensions of the matrix:
size(mymatrix)
The length of the longest size of the matrix. Note, this returns the length of the longest side which could be any dimension:
length(mymatrix)
All of the mathematical operators work on matrices as well. For example:
mean(mymatrix,1)
Takes the mean of the matrix along the first dimension. You could use mean(mymatrix,2) to take the mean along the second dimension and so on.
More useful matrix operators can be found HERE and HERE. Make sure you play around with matrices some more.
Onto the next tutorial!