Next: 1.5 Avoiding carpal-tunnel syndrome:
Up: 1.4 Variables
Previous: 1.4.0.2 Workspace
  Contents
1.4.1 Arrays: vectors and matrices
In the last exercise, you were introduced to arrays. Type, c and you
will see that it is a row vector with three elements. A vector is a
one-dimensional array. Now, type d. d is a vector as well but
a column vector. Use the size command to see the ``size'' and
``shape'' of an array. Try, size(c). We see that c is an
array with one row and 3 columns, a
array. (For one-dimensional
arrays, length also works.) Try
size(d). size tells us that d is a
array,
that is, an array with 4 rows and one column. If you followed that, it
shouldn't be surprising that f is a
array. Note that all
this information is also available using the whos command (or in the
Workspace window if it visible).
Arrays are entered by listing the elements in between square brackets
. Elements in a single row are separated by commas and individual
columns are separated by semicolons (look at the example above again). To
make sure that you understand this, try making two vectors, one column
vector and one row vector. Verify that you've done it correctly by using
the whos command.
Entering arrays and matrices can be made easier by entering them
``visually,'' rather than trying to keep track of all of the commas and
semicolons. Separate individual elements in a row by spaces and individual
rows by a carriage return (that's ``old-timer speak'' for hitting the
return key). For example, try this:
rvec = [1 2 3]
cvec = [4
3
4
2]
mat1 = [3 3 4
4 5 6
9 9 0]
Note that you
don't to have line up all the columns. This works just as well:
rvec = [1 2 3]
cvec = [4
3
4
2]
mat1 = [3 3 4
4 5 6
9 9 0]
Another slick way of defining the values of a one-dimensional array, a
vector, is by using the colon notation. We can define an
array containing the numbers 1-10 by t=[1:10] (Try it.) To go by
twos, try t=[1:2:10] To go by halves, try t=[1:.5:10]. You
could also get all the numbers between 1 and 1000, inclusive, with
t=[1:1000]. Try it and you will see that we need to learn how to define
arrays without MATLAB parroting back to us each value (especially if
more is on). Make MATLAB talk quietly using a semicolon to end a
command. Try, r=[0:.1:pi];. You can verify that MATLAB really did
assign 32 values to the array r by just typing r at the
command line. (Do it.)
Next: 1.5 Avoiding carpal-tunnel syndrome:
Up: 1.4 Variables
Previous: 1.4.0.2 Workspace
  Contents
Gus Hart
2005-01-28