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

From Process Model Formulation and Solution: 3E4
Jump to navigation Jump to search
m
Line 15: Line 15:


We will create these vectors:
We will create these vectors:
* a=[4,5,6,2,3]
* \(a = [4.0, \, 5, \,6, \,-2,\, 3, \text{NaN}, \infty]\)
* b=[0,0,,0] with 300 columns of zeros, one row
* b=[0,0,,0] with 300 columns of zeros, one row
* c=[1,1,,1]T with 300 rows of ones in a single column
* c=[1,1,,1]T with 300 rows of ones in a single column
Line 28: Line 28:
| width="50%" valign="top" |
| width="50%" valign="top" |
<syntaxhighlight lang="matlab">
<syntaxhighlight lang="matlab">
a = [4, 5, 6, -2, 3];
a = [4, 5, 6, -2, 3, NaN, inf];
b = zeros(1, 300);
b = zeros(1, 300);
c = ones(300, 1);
c = ones(300, 1);
Line 46: Line 46:
<syntaxhighlight lang="python">
<syntaxhighlight lang="python">
import numpy as np
import numpy as np
a = np.array([4, 5, 6, -2, 3])
a = np.array([4, 5, 6, -2, 3, np.nan, np.inf])
b = np.zeros((1, 300))  # note the extra brackets!
b = np.zeros((1, 300))  # note the extra brackets!
c = np.ones((300, 1))
c = np.ones((300, 1))

Revision as of 15:38, 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.0,5,6,2,3,NaN,]
  • b=[0,0,,0] with 300 columns of zeros, one row
  • c=[1,1,,1]T with 300 rows of ones in a single column
  • d=[2.6,2.6,,2.6]T with 300 entries of 2.6 in one column
  • e=[4.5,4.6,4.7,,10.5, equi-spaced entries
  • f= 26 entries starting from 3.0, going down -4.0
MATLAB Python
a = [4, 5, 6, -2, 3, NaN, inf];
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, np.nan, np.inf])
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,)