In our discussion of one-dimensional arrays we have discussed the length of the arrays, which we can get using the <span class="key">length</span> function, but we have said little about the <em>shape</em> of the arrays we have created. But you may have noticed that both the <span class="key">linspace</span> and the colon operator <span class="key">:</span> create arrays with one row, and multiple columns :

You may have also noticed that when we generated an array of random numbers using the <span class="key">rand</span> command, we always supplied two arguments to the function, as in

Finally, using square brackets, we constructed arrays as

In each of above examples, we constructed arrays whose length was the number of elements in the array. But in fact, each array also had a shape. Each of the arrays above is a <span class="mathinline">$1 \times N$</span> array, where the 1 indicates the number of rows (one in each case), and <span class="mathinline">$N$</span> is the length of the array. <br/><br/> The power of Matlab comes from its ability to easily manipulate two, three, and higher dimensional arrays. In the above example, neither <span class="key">linspace</span> nor the colon operator, by themselves, can be used to construct higher dimensional arrays. But the <span class="key">rand</span> can. For example,

creates a <span class="mathinline">$3 \times 5$</span> array, or an array with 3 rows and 5 columns. <br/><br/> We can even construct three dimensional arrays with the <span class="key">rand</span> function:

The shape of two dimensional arrays can be characterized by how many rows and columns it has. For every array (including scalars, which, in Matlab, are just <span class="mathinline">$1\times 1$</span> arrays) Matlab stores its dimensions and its number rows and columns. To find the shape of an array, we can use the <span class="key">size</span> function. For example,

Two arrays have the same shape if they have the same number of rows and columns. <br/><br/> If we only ask for one return argument, we will get an array containing the length of both dimensions.

If we ask for the length of an array in which neither dimension is 1, we will get the length of the largest dimension. This comes in handy if you know your array has, for example, many more rows than columns (as if often the case with large data sets) and you simply want to know how many rows of data you have.

In Matlab, even string variables have a length. For example,

We have already seen how we can retrieve a single entry in a one-dimensional array. We simply use the parenthesis operator <span class="mfunc">()</span> with an argument indicating the entry we want.

Retrieve the second entry in <span class="var">v</span>

Retrieving a single entry from a two dimensional array uses the <span class="key">()</span> operator as well, but this time with two arguments, the desired row and column index.

Retrieve the entry in the first row, second column

As with one dimensional arrays, we can also retrieve multiple entries in a two dimensional array at once. For example, to get the second row of the array <span class="var">A</span> above, we use the <span class="key">:</span> operator.

The third column is obtained in analogous fashion:

To obtain rows 1 and 3 at the same time, we can use integer arrays in exactly the same way that we used them in the vector case.

To retrieve columns 2 and 3, we do the following

And finally, if we want the last column or row, we use the <span class="mfunc">end</span> keyword. The last column of A is

and the last row is

The <span class="key">:</span> operator by itself, when used in arrays can be used to retrieve the entire array as one long vector. When used with row vectors, it has the additional effect of converting the array to a column vector.

Suppose we really wanted to see if <span class="var">A</span> is a magic square. We need to see if the rows and columns each add up to the same number. There are many ways to do this in Matlab, but here, we will use a single <span class="key">for</span> loop.

The row sums and the column sums are equal, so <span class="var">A</span> is a magic square.

We use array indexing to not only select values from an array, but also to assign particular values to an array. For example, we can create an <span class="mathinline">$4\times 3$</span> array of all 1s and then reassign the third row to contain values <span class="var">[1,2,3]</span>

The array on the right hand side should have the same shape as the array array indexed on the left hand side. In the above example, the use of the '<span class="var">:</span> allows us to specify all the columns. The array on the left hand side, then should have the same number of columns as those in the array <span class="var">X</span>. <br/><br/> Here is an example in which we re-assign the 'interior' entries of an array the value 7.

We can assign the 'exterior' values of the array to a different value, but this will require two steps. First, we assign the first and last columns the value -3.

And then we assign the top and bottom rows the same value.

There are many instances in which we need to use arrays to store values, say from an iterative calculation that we are doing. In this case, we might use a <span class="var">for</span> loop. The following simple example shows how we might use the series approximation to the function <span class="mathinline">$\sin(x)$</span> to approximate the sine function. <span class="mathdisplay">$$ \sin(x) = \sum_{i=0}^{\infty} \frac{(-1)^{i} x^{2i+1}}{(2i+1)!} $$</span> We can show how the approximations get closer and closer to our desired value, and plot the convergence of the sum.

Because we stored the values in an array, we can now plot our approximations.

We already had a simple introduction to array concatenation for one-dimensional arrays. Now let's extend this idea to two dimensional arrays. First, it will be convenient to use the square brackets to build column vectors as well as row vectors. We have already seen that

builds a row vector. Using <span class="key">[]</span> in a similar fashion, we can also create a column vector. This time, however, we must separate each entry by a semi-colon (<span class="key">;</span>) to indicate that we want to "stack" the numbers on top of each other.

In Matlab, this way building of arrays from existing components (numbers, in this case), is called "concatenation". Concatenation can either be done horizontally (as was done in the first example above) or vertically (as in the second example). </br><br/> This idea of array concatenation extends naturally to situations where the components are not just numbers, but are themselves arrays. For example, suppose we wanted to build an array by stacking existing row vectors on top of each other. This can easily be done using vertical concatenation.

In a similar fashion, we can build an array from existing column vectors.

Of course, array concatenation can fail if you try to stack arrays that are not "conformable", i.e. their sizes don't match. Try the following :

<pre class="codeinput"> A = [1; [2 3]] </pre> <pre class="codeoutput"><span class="error"> Error using lab_5 (line 315) Error using vertcat CAT arguments dimensions are not consistent.</span> </pre>

In this case, we are trying to stack a single number on top of a 1x2 row vector. The number of columns in the components we are trying to stack do not match, and Matlab complains.

The following, however, works

We can always horizontally concatenate two row vectors of different sizes.

Often, data will be stored in a local file on your harddrive. You can construct an array from this file by simply loading this file into Matlab using the <span class="var">load</span> command. For example, suppose we have the file <tt>my_data.dat</tt> stored in the local working directory. We can load this file into an array called <span class="var">A</span> using the command <pre class="codeinput"> A = load('my_data.dat'); </pre> <p>The array <span class="var">A</span> will then have the same number of rows and columns as the file. For example, suppose that the file 'A.dat' looks like this <pre class="file"> % File A.dat 3.4 5.6 4.7 8.9 -5.1 6.7 -2.1 5.6 </pre> <p>Then, we can load the file and assign the results to the variable <span class="var">A</span>.

The file name and the variable name do not have to match. Also, comments can be included in data files in the same way that they can be included in m-files.

<div class="prob"> Construct the arrays described below, using Matlab functions, array assignment, or array concatenation. <ol> <li class="ex">A <span class="mathinline">$10\times1$</span> array of all 1s.</li> <li class="ex">A <span class="mathinline">$2\times 9$</span> array in which the entries in the first row are all equal to <span class="mathinline">$\pi$</span> and the entries in the second row are all equal to <span class="mathinline">$e$</span>.</li> <li class="ex">A <span class="mathinline">$5\times3$</span> array of random numbers.</li> <li class="ex">A <span class="mathinline">$5\times3$</span> array of random numbers between -1 and 1.</li> <li class="ex">A <span class="mathinline">$3\times4$</span> array of all 1s, with the exception of the second row, which has been replaced by a row of -4s.</li> <li class="ex">A <span class="mathinline">$7\times5$</span> array, in which the first 6 rows are random numbers between -1 and 1, and the last row is chosen so that the sum of all the rows is exactly a row of 0s.</li> <li class="ex">A <span class="mathinline">$10\times 10$</span> array with 1s in the first columm, 2s in the second column, 3s in the third column and so on. </li> <li class="ex">A <span class="mathinline">$7\times7$</span> array in which the outer 'layer' of values is 0, the next layer is all 1s, and the innermost entry is 3. </li> </ol> </div> <br/><br/> <div class="prob"> Using a <span class="var">for</span> loop, demonstrate the convergence of the series expansions of the following functions. Evaluate each function at the indicated value. <ol> <li class="ex"><span class="mathdisplay">$$\cos(x) = \sum_{n=0}^{\infty}\frac{(-1)^{n}x^{2n}}{(2n)!}$$</span> at <span class="mathinline">$x=5.4$</span>.</li> <li class="ex"><span class="mathdisplay">$$\exp(x) = \sum_{n=0}^{\infty} \frac{x^n}{n!}$$</span> at <span class="mathinline">$x=0.2$</span>.</li> <li class="ex"><span class="mathdisplay">$$\ln(1+x) = \sum_{n=0}^{\infty} \frac{(-1)^{n-1}x^n}{n}$$</span> at <span class="mathinline">$x=-0.5$</span>.</li> </div>

<p> Compare your answers with the <a href="lab_5_solns.html">solutions</a>.</p>