MATLAB for Machine Learning
上QQ阅读APP看书,第一时间看更新

Familiarizing yourself with the MATLAB desktop

MATLAB is an interactive working environment based on the matrix, the most natural way to express computational mathematics. Moreover, it is a programming language designed for technical computing, mathematical analysis, and system simulation. The integrated graphics make it easy to view and acquire knowledge from the data. Finally, a large library of prebuilt toolboxes allows us to immediately start with programming.

After installing, to start MATLAB, simply double-click on the icon on the desktop; or from the system prompt, type matlab. At first startup, MATLAB will show us a desk of contents with all the necessary items for its proper and smooth operation. Figure 2.1 shows the MATLAB desktop in the R2017a version:

Figure 2.1: MATLAB R2017a desktop for Windows 10

The MATLAB desktop includes the following panels:

  • Current Folder: Shows the files that are stored in this folder (we can change it)
  • Command Window: At the prompt (>>), any MATLAB command or expression can be entered, so it can be used interactively
  • Workspace: Shows variables that have been created or imported from data files or other programs
  • Command History: Displays commands that have been entered (they can be called up quickly)

For a comprehensive introduction to the MATLAB desktop, we can use the following commands:

  • help: Lists all primary help topics in the command window
  • lookfor: Searches for a keyword in all help entries
  • demo: Accesses product examples in the Help browser
  • doc: A reference page in the Help browser

To exit from MATLAB, either type quit or exit at the prompt or click the Close box in the MATLAB desktop. The following figure shows the MATLAB Toolstrip:

Figure 2.2: MATLAB Toolstrip

MATLAB now uses the typical toolstrip ribbon interface found in the recent Microsoft Windows applications. At the top of desktop window is the MATLAB Toolstrip (Figure 2.2). The toolstrip organizes MATLAB functionality in a series of tabs. Tabs are divided into sections that contain a series of related controls. The controls are buttons, drop-down menus, and other user interface elements that we will use to perform operations in MATLAB.

For example, the HOME tab includes icons used to perform operations on files, variables, code, and so on. The PLOTS tab shows tools for creating graphic presentations of our formulas and the APPS tab allows us to get new applications to use with MATLAB.

The first way--and a simple way--to interact with MATLAB is by using it as a calculator. Suppose we want to find the sum of two numbers, 10 and 90. To perform this operation, just position the cursor to the right of the symbol >> (MATLAB prompt), write precisely 10+90, and then press the Enter key, thereby obtaining the result:

>>10+90
ans =
100
>>

The following screenshot shows a typical math task in the MATLAB desktop:

Figure 2.3: Typical  math task in the MATLAB desktop

MATLAB performs the math task and assigns the result to the ans variable. Since we did not specify an output variable, MATLAB uses a default variable, precisely ans (short for answer), to store the results of the calculation. This operation is performed by default for every subsequent operation, with the result that the content of this ans variable, which could be defined as a working variable, is overwritten at each occurrence.

In order to avoid losing such information, starting from the next calculation, we will define new variables.

After this simple task, MATLAB starts to fill its windows; in fact, in the Workspace Window appears the ans variable, while the Command History window shows the operation performed so far (Figure 2.3).

To define a new variable in order to preserve the contents, we can use an assignment statement. For example, create two variables named FIRST and SECOND by typing the following statement in the command line:

>> FIRST = 10
FIRST =
10
>> SECOND = 90
SECOND =
90
>>

Now we can add these two variables and save the result in a third variable named THIRD:

>> THIRD = FIRST + SECOND
THIRD =
100
>>

As we know, the basic elements of MATLAB are arrays, so all of the entered information is stored in the form of arrays. Thus, the variables FIRST, SECOND, and THIRD are arrays with a single value. To create an array with ten elements in a single row (row vector), separate the elements with either a space as shown in the following code or a comma (,):

>> vector = [10 20 30 40 50 60 70 80 90 100]
vector =
10 20 30 40 50 60 70 80 90 100
>>

Similarly, to create a matrix that has multiple rows, separate the rows with semicolons, as follows:

>> matrix = [10 20 30; 40 50 60 ;70 80 90]
matrix =
10 20 30
40 50 60
70 80 90
>>

To access the elements of an array, use indexing; for example, we can specify row and column subscripts so as to select the element in the first row and second column:

>> matrix (1,2)
ans =
20
>>

To select multiple elements of an array, use the colon operator, specifying a interval of the form start:end. For example, for selecting the elements in the first three rows and the third column we can use the following code:

>> matrix (1:3,3)
ans =
30
60
90
>>

Omitting start or end values, we will specify all the elements in that dimension. Thus, the previous result can also be obtained in the following way:

>> matrix (:,3)
ans =
30
60
90
>>

The colon operator can also be used to create an equally spaced vector of values using the more general form start:step:end. For example, to create an array containing even numbers from 0 to 20, we can do this:

>> vector_even = 0:2:20
vector_even =
0 2 4 6 8 10 12 14 16 18 20
>>

To manipulate the data in the workspace, the following three commands are particularly useful: who, whos, and clear. The first and second list the contents of the workspace with the difference in the report details, while the third one cleans it by deleting all the variables. So far we have used some variables; let's see:

>> who
Your variables are:
FIRST SECOND THIRD matrix vector vector_even
>> whos
Name Size Bytes Class Attributes
FIRST 1x1 8 double
SECOND 1x1 8 double
THIRD 1x1 8 double
matrix 3x3 72 double
vector 1x10 80 double
vector_even 1x11 88 double
>>

So, we have verified that the whos command shows details of the variables contained in the workspace. How is it possible to check? The variables present in the whos report actually represent those contained in the MATLAB workspace, as shown in the Figure 2.4:


Figure 2.4: Workspace variables list.

It’s time to tidy up the workspace, removing all of its contents. To do this, we will use the clear command, as follows:

>> clear
>> who
>>

Later, we use the who command to confirm that the workspace is empty. Use the clear command with caution. The variables that you erase may be impossible to recover. It's a good practice to save contents of the workspace before we exit MATLAB because variables do not persist later. Save your data for later use with the save command, shown as follows:

>> save filename.mat

With this command all the contents of the workspace is saved in a compressed file with a .mat extension, called a MAT-file, and all the contents of the workspace. To restore data from a MAT-file into the workspace, simply use the load command:

>> load filename.mat

MATLAB provides a large number of functions that perform computational tasks. For example, we calculate the average of the values contained in the array previously used. To call a function such as mean(), enclose its input arguments in parentheses, as shown here:

>> vector
vector =
10 20 30 40 50 60 70 80 90 100
>> mean(vector)
ans =
55
>>