<?xml version="1.0" encoding="utf-8" standalone="no"?>
<!DOCTYPE document PUBLIC "-//CNX//DTD CNXML 0.5 plus MathML//EN" "http://cnx.rice.edu/technology/cnxml/schema/dtd/0.5/cnxml_mathml.dtd">
<document xmlns="http://cnx.rice.edu/cnxml" xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/" xmlns:m="http://www.w3.org/1998/Math/MathML" id="new">
  <name>Vectors and Arrays in M-File Environments</name>
  <metadata>
  <md:version>1.2</md:version>
  <md:created>2006/02/10 13:55:20 US/Central</md:created>
  <md:revised>2006/09/04 16:03:19.081 GMT-5</md:revised>
  <md:authorlist>
      <md:author id="morrell">
      <md:firstname>Darryl </md:firstname>
      
      <md:surname>Morrell</md:surname>
      <md:email>morrell@asu.edu</md:email>
    </md:author>
  </md:authorlist>

  <md:maintainerlist>
    <md:maintainer id="morrell">
      <md:firstname>Darryl </md:firstname>
      
      <md:surname>Morrell</md:surname>
      <md:email>morrell@asu.edu</md:email>
    </md:maintainer>
  </md:maintainerlist>
  
  <md:keywordlist>
    <md:keyword>Array</md:keyword>
    <md:keyword>M-File</md:keyword>
    <md:keyword>MathScript</md:keyword>
    <md:keyword>MATLAB</md:keyword>
    <md:keyword>Octave</md:keyword>
    <md:keyword>Vector</md:keyword>
  </md:keywordlist>

  <md:abstract>This module provides a totorial introduction to using vectors in an m-file environment, provieds a link to the Mathworks tutorial on MATLAB vectors and arrays, and adds a useful piece of information not covered in the tutorial.</md:abstract>
</metadata>
<content>
    <section id="id1">
        <name>Vectors and Arrays in M-File Environments</name>
        <para id="Blahblah">
            One significant capability of environments accounts for much of their popularity among engineers: their ability to do vector and matrix computations.  M-file environments can operate on the following types of values:
            <list id="id13" type="named-item">
                <item><name>Scalar</name> a scalar is a single value (i.e. a number).</item>
                <item><name>Vector</name> a vector is an ordered series of numbers.</item>
                <item><name>Matrix</name> a matrix is a rectangular array of numbers.  <note>The ability to do computations on vectors and matrices gives MATLAB its name (MATrix LABoratory).</note>  </item>
                <item><name>String</name> variables may also contain strings of characters.</item>
            </list>
        </para>
    </section>
    <section id="Vec1">
        <name>Vector Basics</name>
        <para id="bob1">
            There are several ways to create a vector of values. One is to enclose the values in square brackets. For example, the command <code>[9 7 5 3 1]</code>  creates the vector of values 9, 7, 5, 3, and 1.  This vector can be assigned to a variable <code>v</code>:
<code type="block">
&gt;&gt; v = [9 7 5 3 1]

v =

  9  7  5  3  1
</code>
        </para>
        <para id="bob2">
            A second way to create a vector of values is with the sequence notation <code>start:end</code> or <code>start:inc:end</code>. For example, <code>1:10</code> creates the vector of integers from 1 to 10:
<code type="block">
&gt;&gt; 1:10

ans =

   1   2   3   4   5   6   7   8   9  10
</code>
            The command <code>1:0.1:2</code> creates the vector
<code type="block">
&gt;&gt; 1:0.1:2

ans =

  1.0000  1.1000  1.2000  1.3000  1.4000  1.5000  1.6000  1.7000  1.8000  1.9000  2.0000
</code>
            The command <code>10:-1:1</code> creates the vector
<code type="block">
&gt;&gt; 10:-1:1

ans =

  10   9   8   7   6   5   4   3   2   1
</code>
        </para>
        <para id="bob3">
            Vector elements are accessed using numbers in parentheses. For example if the vector <code>v</code> is defined as <code>v = [9 7 5 3 1]</code>, the second element of <code>v</code> can be accessed as
<code type="block">
&gt;&gt; v(2)
    ans = 7
</code>
            The fourth element of <code>v</code> can be changed as follows:
<code type="block">
&gt;&gt; v(4) = 100

v =

    9    7    5  100    1
</code>
        </para>
    </section>
    <section id="vecc">
        <name>Element by Element Operations on Vectors</name>
        <para id="bob9">
            In addition to vector and matrix arithmetic, many operations can be performed on each element of the vector.  The following examples use the vector <code>v = [9 7 5 3 1]</code>.
            <list id="dd11" type="named-item">
                <item><name>Addition</name> the command <code>v+val</code> adds <code>val</code> to each element of <code>v</code>:
<code type="block">
&gt;&gt; v+5
    ans =

      14  12  10   8   6

</code>
                </item>
                <item><name>Subtraction</name> the command <code>v-val</code> subtracts <code>val</code> from each element of <code>v</code>:
<code type="block">
&gt;&gt; v-5
    ans =

      4   2   0  -2  -4

</code>
                </item>
                <item><name>Multiplication</name> the command <code>v*val</code> multiplies each element of <code>v</code> by <code>val</code>:
<code type="block">
&gt;&gt; v*5
    ans =

      45  35  25  15   5

</code>
                </item>
                <item><name>Division</name> the command <code>v/val</code> divides each element of <code>v</code> by <code>val</code>:
<code type="block">
&gt;&gt; v/5
    ans =

      1.80000  1.40000  1.00000  0.60000  0.20000

</code>
                The command <code>val./v</code> divides <code>val</code> by each element of <code>v</code>:
<code type="block">
&gt;&gt; 5./v
    ans =

      0.55556  0.71429  1.00000  1.66667  5.00000

</code>
                </item>
                <item><name>Exponentiation</name> the command <code>v.^val</code> raises each element of <code>v</code> to the <code>val</code> power:
<code type="block">
&gt;&gt; v.^2
    ans =

      81  49  25   9   1

</code>
                </item>
            </list>
        </para>
    </section>
    <section id="sss3">
        <name>More Information on Vectors and Matrices</name>
        <para id="delete_me">
            An excellent tutorial on how to use MATLAB's vector  and array capabilities is at the <link src="http://www.mathworks.com/academia/student_center/tutorials/performing_calculations.html">Mathworks MATLAB tutorial page.</link>
        </para>
        <para id="element-679">
            One useful method of accessing entire rows or entire columns of the matrix is not mentioned in the tutorial. Suppose that the matrix <code>A</code> is defined as
<code type="block">
&gt;&gt; A = [1  2  3  4  5
        6  7  8  9  10
        11 12 13 14 15
        16 17 18 19 20]        

A = 

        1  2  3  4  5
        6  7  8  9  10
        11 12 13 14 15
        16 17 18 19 20
        
</code>
            An entire row of <code>A</code> can be obtained by specifying a single ":" as the column index:
<code type="block">
&gt;&gt; A(2,:)

    ans = 

        6  7  8  9  10

</code>
            Similarly, an entire  column of <code>A</code> can be obtained by specifying a single ":" as the  row index:
<code type="block">
&gt;&gt; A(:,3)

ans = 

     3
     8
    13
    18

</code>
        </para>   
    </section>
  </content>
  
</document>
