Kickstarting Matlab – lesson 1

I know this could be useful to me. Everybody is using Matlab around me. But that’s such a big time commitment. How should I learn this language? How long is it going to take for me to do something useful?

or even

Do you think I am too old to learn this stuff?

Many people in the scientific world with no background in computer science want to start using Matlab. They know it will open so many doors to their data analysis but they are scared of the time they might lose in the learning phase. Or they are scared of being so lost in the process that they just abandon.

Well, the first thing to know is : Don’t panic! Using Matlab is actually quite easy.

And, as always, your learning curve is going to be very steep at the beginning. Matlab is actually designed for you, users with no background in programming.

And there are no limit in what you can do with it!

But as you need some help, I am going to set up a simple path for you to get started.

Where to start? My suggestion would be : Start with something real right away. Look around you, there must be a very simple thing you have always wanted to do in Matlab. You need a real and immediate application of your Matlab new skills to get in a positive learning circle.

But before that, I need to quickly inject in your brain the fundamentals. So let’s get started!

I am going to assume you have a working installation of Matlab on your computer.

If you start Matlab, you should get something that look like this :

This is the default layout of Matlab.

The main window is the Command Window. You can type in there any command that are available in Matlab.

The second window in importance is the workspace. This is the current state of memory in Matlab. All the variable that are being used go there. The command history and the current folder are just useful tool that you can use but they are not essential to understand Matlab.

Now that we have a working environment set up. Why not type in your first command?

Matlab was initially designed for one thing and one thing only : deal with data, real data. As a result, the most fundamental object in Matlab is a matrix. Matrix can be of many sizes and types as you will see. But first, since Matrix is the fundamental object, we need one to play with.

Please type in the command window :

X=1:10:100

You should get this in return :

>> X=1:10:100

X =

1 11 21 31 41 51 61 71 81 91

This is quite a simple command but actually Matlab is doing a LOT of things here.

First, you tell him to create a matrix named X. And then you tell him to fill it with 1:10:100. Please note that now X is your workspace.

To understand what this command means, you need to know about this fundamental character, the colon (:). I am going to let Matlab itself explain it to you. Type in :

help Colon

Now you get a lot of help!

If you read this carefully, it is telling you that here, the Colon operator is creating a matrix using 1, 10 and 100 as arguments. 1 is the first element, 100 the last and 10 the step. So in essence it creates this matrix.

It is also telling you that you could also have typed in colon(1,10,100) to get exactly the same result (actually absolutely nobody uses it in this form but I am just mentioning for the advanced Matlab folks that might have never heard about this usage).

Now that we have a matrix. let’s play with it.

type in

X(3)

This display you the third element in the matrix.

You can also do

X+1

or

2.3*X

or

X+X

or

exp(X)

this last one being the exponential of X.

At this stage you might have noticed this “ans” coming around both in the command window and in the workspace. If you type in :

help ans

It tells you that ans is just the most recent answear, in other words, ans is the last variable created. To bypass ‘ans’, you need to assign the result to another variable, like so :

Y=exp(X)

At this stage, all the results were fully displayed. But this might be annoying, like when you do :

X=1:0.1:1000

It is just too much! And besides, you know what is in X, right? So instead use :

X=1:0.1:1000;

the semicolon ‘;’ suppress the output to the command window.
You already can do quite some calculation in Matlab. In my next post, I will go over how to use the Editor to write down more complicated code.

2. Store the Square of this value in another variable using for loop.
Suppose, another variable name, a
Output: a= 784 841 900 …..

3. Write a function with one input and one output argument. The output argument returns the 4th power of input argument.
Suppose the function name is do_4_squre, then
Output: do_4_squre(2)=16
do_4_squre(3)=81;

4. Write a program to compute 4th power of the elements of a vector using the previous defined function and for loop.
Suppose a variable,
V=1 2 3
Output
B= 1 16 81