>> x = 2 + 3x = 5>> ansans = 4
At this point the workspace has two variables
. The development environmenthas workspace editor which allows you to examine the variables in the workspace and howmuch space they consume. You can clear the workspace to its original state by typing thecommand
1.2.2 Loading scripts from ﬁles
You can use the M
built-in editor to write scripts, save them to ﬁles, and then havethem loaded and evaluated as if you typed the commands directly into the command line.Also, while in the command line shell, you can type the ﬁle name containing a M
script to achieve the same effect. Text ﬁles containing M
code are called m-ﬁles andhave the ﬁle extension “
”.As you develop a script, you will follow a cycle of editing and loading. When youreload your script, the workspace from the previous cycle will still be in effect unless youclear it. You probably want to clear it to avoid subtle reinitialization errors. The best wayto do this is to put the
command as the ﬁrst line of the script, so that the script runsin a virgin workspace.
2 Vectorizing code
scripts achieve efﬁciency in speed of execution by using large instruc-tions to reduce decoding overhead and by using space to decrease time. For a given problemsize, M
scipts use a lot of memory. Thus M
trades memory to increase speed.
The primary data structure in M
is a matrix. The appendix in this tutorial has moreinformation about matrices. You can create and initialize a matrix by typing the matrixvalues in an assignment statement. The example below creates a
matrix of double-precision ﬂoating-point values and then sets the variable
to reference the matrix.
Noticethat the variable
was not previously declared. The rows of the matrix are terminated withsemicolons. All the commands below create the same matrix.
>> w = [1 2 3; 4 5 6]>> w = [1 2 3; 4 5 6;]>> w = [1, 2, 3; 4, 5, 6]>> w = [1, 2, 3; 4, 5, 6;]
Numbers in M
are always double-precision ﬂoating-point.