Difference between revisions of "Software tutorial/Vectors and arrays"

From Process Model Formulation and Solution: 3E4
Jump to navigation Jump to search
m (Created page with "In this section we will focus on containers for your numbers: '''vectors''' and '''arrays'''. For the purposes of this section you should use this terminology: ; Vector : A on...")
 
m
Line 11: Line 11:


In this course we will mainly use vectors and matrices, however, arrays are extremely prevalent in process modelling.
In this course we will mainly use vectors and matrices, however, arrays are extremely prevalent in process modelling.
== Creating vectors ==
We will create these vectors:
* \(a = [4, 5, 6, -2, 3]\)
* \(b = [0, 0, \ldots, 0]\) with 300 columns of zeros, one row
* \(c = [1, 1, \ldots, 1]^T\) with 300 rows of ones in a single column
* \(d = [2.6, 2.6, \ldots, 2.6]^T\) with 300 entries of 2.6 in one column
* \(e = [4.5, 4.6, 4.7, \ldots, 10.5 \), equi-spaced entries
* \(f = \) 26 entries starting from 3.0, going down -4.0
{| class="wikitable"
|-
! MATLAB
! Python
|-
| width="50%" valign="top" |
<syntaxhighlight lang="matlab">
a = [4, 5, 6, -2, 3];
b = zeros(1, 300);
c = ones(300, 1);
d = ones(300, 1) .* 2.6;
e = 4.5:0.1:10.5;
f = linspace(3.0, -4.0, 26);
>> size(c)
ans =
  300    1
>> size(f)
ans =
    1    26
</syntaxhighlight>
| width="50%" valign="top" |
<syntaxhighlight lang="python">
import numpy as np
a = np.array([4, 5, 6, -2, 3])
b = np.zeros((1, 300))  # note the extra brackets!
c = np.ones((300, 1))
d = np.ones((300, 1)) * 2.6
e = np.arange(4.5, 10.5001, 0.1);  # type help(np.arange) to understand why
f = np.linspace(3.0, -4.0, 26)
>>> c.shape
(300, 1)
>>> f.shape
(26,)
</syntaxhighlight>
|}

Revision as of 15:36, 26 September 2010

In this section we will focus on containers for your numbers: vectors and arrays. For the purposes of this section you should use this terminology:

Vector
A one-dimensional list of numbers
Array
A multi-dimensional arrangement of numbers
Matrix
A two-dimensional array

In this course we will mainly use vectors and matrices, however, arrays are extremely prevalent in process modelling.

Creating vectors

We will create these vectors:

  • \(a = [4, 5, 6, -2, 3]\)
  • \(b = [0, 0, \ldots, 0]\) with 300 columns of zeros, one row
  • \(c = [1, 1, \ldots, 1]^T\) with 300 rows of ones in a single column
  • \(d = [2.6, 2.6, \ldots, 2.6]^T\) with 300 entries of 2.6 in one column
  • \(e = [4.5, 4.6, 4.7, \ldots, 10.5 \), equi-spaced entries
  • \(f = \) 26 entries starting from 3.0, going down -4.0
MATLAB Python
a = [4, 5, 6, -2, 3];
b = zeros(1, 300);
c = ones(300, 1);
d = ones(300, 1) .* 2.6;
e = 4.5:0.1:10.5;
f = linspace(3.0, -4.0, 26);

>> size(c)
ans =
   300     1
>> size(f)
ans =
     1    26
import numpy as np
a = np.array([4, 5, 6, -2, 3])
b = np.zeros((1, 300))   # note the extra brackets!
c = np.ones((300, 1))
d = np.ones((300, 1)) * 2.6
e = np.arange(4.5, 10.5001, 0.1);  # type help(np.arange) to understand why
f = np.linspace(3.0, -4.0, 26)

>>> c.shape
(300, 1)
>>> f.shape
(26,)