Part 1

This section will introduce you to the basics of programming with a specific emphasis on programming in Matlab. We‘ll cover the basics of the syntax and the basic data structures.

true

Finding our way around

In this section, we’ll focus on finding our way around the Matlab gui


Ex

To get comfortable with Matlab, the first thing we’ll try to do is to use Matlab as a calculator just by typing commands at the command prompt.

Try something like

Ex

In the previous example, you typed in the numbers directly. Let’s try do it again with variables. Create a variable called shoe_size and assign your shoe size to it. Create another variable called my_age and assign your age to it.

Now divide your age by your shoe size!

Ex

Knowing how to get help is the one of the most important things you can learn. For this task I’d like you to explore the help command and the doc command.

Try getting help on the function called ttest. Use the help command and see what information it gives you. Then try using the doc command and seeing what information it gives you.

Basic syntax and data structures

In this section, we’ll learn some of the basics of Matlab, and how to work with data in Matlab.


The variables and data types we’ll learn about include:

Practical activities

Now that we have a basic understanding the Matlab interface and the Matlab syntax we can put some of that knowledge into practice.

Creating and modifying matrices

Ex

Let’s start off my trying to create a simple 3 × 4 matrix. It should look something like this:

\[ \mathrm{my\_matrix} = \left[\begin{array}{cc} 11 & 12 & 13 & 14\\ 21 & 22 & 23 & 24\\ 31 & 32 & 33 & 34\\ \end{array}\right] \]

You’ll be able to check that you’ve created a matrix of the correct size by running the code below. If you’ve done it correctly then the size function should return a 1 × matrix will the values [3 4]

Ex

Use the code below to create a matrix. Once you’ve created the matrix, use logical indexing to replace all the values that are equal to 99 with the value NaN (not a number).

Ex

We haven’t covered the find() function yet, but it does something that you might sometimes find useful. Use the code below to create a matrix

Try using logical indexing find the elements of a_row_vector that are greater than 100. If you’ve done it correctly you should see something like this when you run the code…

Next, try return the elements from a_row_vector that match your logical rule…

So far we’ve got the matrix that tells us which elements match and which don’t and we’ve got the actual values of the elements that match. But what if we wanted to know the index of the elements that matched our logical true? Try the code below and see what happens…

What does it return? What does it mean?

Ex

All the logical operations we’ve used so far have been fairly simple. We’ve only been matching one condition. But let’s return to our variable a_row_vector. It has some values greater than 100 and some values less that 20, but does it have any values between those two conditions? Can you figure out what those values are? Try joining to logical condition and & (logical AND)

Ex

In the previous exercise we discovered logical AND. But there’s also logical OR. Logical OR is the |.

Using logical OR find the values in a_row_vector that are either less than 20 or greater than 100.

Ex

Sometimes we might have two vectors/matrices that we want to join together. To do this, we’ll want to concatenate the vectors/matrices together. There’s a two different ways to do this in Matlab. One way is to use the vertcat() and horzcat() functions, and the other way is to wrap the individual vectors/matrices in [].

Try using help or doc to get help on the vertcat() and horzcat() functions, and then use them to concatenate the two vectors below.

Next try using [] to concatenate the two vectors. If you want a hint on how to use [] to concatenate vectors check back on creating vectors/matrices.

Ex

Create a struct with the following fields

  1. name

  2. id_code

  3. rt

Fill the name field with a name (pick any name you want). Put a four digit number as the id_code field. Make sure this is char. And in the rt field, put a matrix with 2 columns and 10 rows. The first column should have consecutive numbers from 1 to 10, and the second column should just random numbers.

Hints: You try the code below to fill a row vector with the numbers 1 to 5

We probably need a column vector. Try the code below to turn the row vector vect into a column vector.

To generate some random numbers, read the help on the randn() function.

Ex

This last exercise is more a demonstration that a problem for you to solve. The aim is to demonstrate something about how computers represent decimal fractions.

Let’s first create a variable called X and set it equal to 0.1 + 0.2

Now create another variable called Y and set it equal to 0.3

Now that you’ve created these two variables compare them with a logical operator to see if they’re equal?

What is the result?

This might seem surprising, but it’s a fundamental problem of computers that they can’t represent decimal fractions exactly. Because of this, inaccuracies creep in when you try and do maths with them. You can read more about it on the wikipedia on Floating-point arithmetic. You don’t need to understand the exact details of why it occurs, just that it does sometimes occur! And this means that you must be careful when comparing numbers that aren’t whole numbers. In fact, it’s best to never do it.

We’ll return to this problem in a later session, after we’ve learned how to make functions. We’ll try to make a function that will give use a sensible answer when we compare 0.3 and 0.1 + 0.2