3-Creating Arrays - For Students - Mid Term

Download as pdf or txt
Download as pdf or txt
You are on page 1of 36

MATLAB: An introduction

with applications
3. Creating Arrays
Context of This Chapter
• Array: fundamental form that MATLAB uses to store and
manipulate data
• A list of numbers arranged in rows and/or columns

• One-dimensional arrays: Vectors

• Two-dimensional arrays: Matrices

• How to create and address arrays

• Arrays can also be a list of characters (strings).


Creating a One-Dimensional Array
(Vector)
• One-dimensional array: a list of numbers arranged in a
row and a column.
– Ex) the position of a point in a 3D Cartesian coordinate
system. [2 5 6] z
P (2,5, 6)

• Population growth data can be used to


x y
create two lists of numbers.
>> year = [1984 1986 1988 1990 1992 1994 1996]
>> pop = [127 130 136 145 158 178 211];

Year 1984 1986 1988 1990 1992 1994 1996


Population 127 130 136 145 158 178 211
Creating a Vector
variable_name = [type vector elements]

• The vector is created by typing the element inside square


brackets [ ].

• Row vector: Type the elements with a space or a comma


between the elements. [1 2 3]

• Column vector: Type the left square bracket [ and then


enter the elements with a semicolon between them, or
press the Enter key after each element. Type the right
square bracket ] after the last element.
Column Vector

>> v = [3; 4; 5] >> v = [3 >> v = [3 4 5]'


v=3 4 v=3
4 5] 4
5 v=3 5
4
5
Creating a Vector with Constant
Spacing

• First term: m, spacing: q (default:1), last term: n

variable_name = [m : q : n] or variable_name = m : q : n

>> x = [1:2:8] >> x=(0:0.5:1)*pi


x=1 3 5 7 x=0 1.5708 3.1416
>> x=15:-3:8 >> t= -1:0.5:1
x=15 12 9 t=-1.0000 -0.5000 0 0.5000 1.0000
Creating a Vector with Linear
Spacing
• xi: first element, xf: last element, n: number of elements
(default: 100)

variable_name = linspace(xi, xf, n)

>> x = linspace(2, 14, 6) % 6 elements, first element 2, last 14


x = 2.0000 4.4000 6.8000 9.2000 11.6000 14.0000
>> delta_x = (14-2)/5
delta_x = 2.4
>> t = 2:delta_x:14
t = 2.0000 4.4000 6.8000 9.2000 11.6000 14.0000
>> y = linspace( 0.1, 10) % The default of number of elements is 100
y = 0.1000 0.2000 0.3000 … 9.8000 9.9000 10.0000
logspace
• logspace function creates a logarithmically spaced vector

• logspace(x,y,n) creates a vector with n values in the inclusive


range from 10^x to 10^y.
Array Addressing
• Elements in an array can be addressed
individually or in subgroups.

• When there is a need to redefine only some of


the elements.

• When specific elements are to be used in


calculations.

• When a subgroup of the elements is used to


define a new variable.
Referring to and Modifying Elements

• The elements in a vector are numbered


sequentially; each element number is called the
index, or subscript.
Referring to and Modifying Elements

newvec(5) would be pronounced


“newvec sub 5”,
Referring to and Modifying Elements

The vector [1 10 5] is called an index vector;


Referring to and Modifying Elements
Referring to and Modifying Elements
Array Addressing- Vector
• The address of an element in a vector is its
position in the row (or column).

• For a vector v, v(k) refers to the element in


position k.

• If v=[12 8 9 6 28], v(1)=12, v(3)=9, v(5)=28

• A single vector element, v(k), can be used just


as a variable.
>> VCT=[35 46 78 23 5 14 81 3 55] % Define a vector.
VCT =
35 46 78 23 5 14 81 3 55
>> VCT(4) % Display the forth element.
ans =
23
>> VCT(6)=273 % Assign a new value to the sixth element
VCT=
35 46 78 23 5 273 81 3 55
>> VCT(2)+VCT(8) % Use the vector elements in mathematical
expressions.
ans =
49
>> VCT(5)^VCT(8)+sqrt(VCT(7))
ans =
134
Creating a Two-Dimensional Array
(Matrix)
• Matrix has numbers in rows and columns.
• All the rows must have the same number of elements.

 2 4 10 Enter key


A = 16 3 7 
 
8 12 35
>> A=[2 4 10
16 3 7
>> A=[2 4 10; 16 3 7; 8 12 35] 8 12 35]
A= A=
2 4 10 2 4 10
16 3 7 16 3 7
8 12 35 8 12 35
Creating matrices

>> x=4; y=2; z=8; >> A=[2 4 10;linspace(8, -2, 3)]


A=
>> A=[x y z; sin(x/z) x^2 x+y]
2 4 10
A= 8 3 -2
4.0000 2.0000 8.0000 >> A=[2 4 10; 8: -5: -2 ]
0.4794 16.0000 6.0000 A=
>> A=[1:2:11; 0:5:25; 2 4 10
8 3 -2
linspace(10, 60, 6);67 2 43 68 4 13]
>> A=[A; 10 20 30]
A= A=
1 3 5 7 9 11 2 4 10
0 5 10 15 20 25 8 3 -2
10 20 30 40 50 60 10 20 30
67 2 43 68 4 13
The Zeros, Ones, and, Eye
Commands
• zeros(m, n): matrix with m rows and n columns in which all
elements are 0.
• ones(m, n): all elements are 1.
• eye(n): identity matrix. Square matrix with nrows and n columns
in which diagonal elements are 1 and the rest are 0.

>> Z = zeros(2 , 3) >> I = eye(3)


Z= I=
0 0 0 1 0 0
0 0 0 0 1 0
>> O = ones(2, 2) 0 0 1
O=
1 1
1 1
Notes about Variables in
MATLAB
• All variables are arrays. A scalar is an array with one
element.

• The variable (scalar, vector, or matrix) is defined by


the input when the variable is assigned.

• Once a variable exists, it can be changed to any other


size, or type, or variable.
Transpose Operator (‘)
• Switches a row (column) vector to a column (row)
vector.

>> v=[10; 20; 30]; >> A=[1 2 3; 10 20 30]


>> vt = v’ A=
vt = 1 2 3
10 20 30 10 20 30
>> x=[10 20 30]; xt = x’ >> B=A’
xt = B=
1 10
10
2 20
20
3 30
30
Array Addressing- Matrix
• The address of an element in a matrix is its position,
defined by the row number and the column number
where it is located.

• A(k,p) refers to the element in row k and column p.

 5 10 9 8 
A = 18 1 7 11
29 14 3 6 

• A(1,1)=5, A(2,3)=7
Array Addressing- Matrix

>> A = [5 10 9 8; 18 1 7 11; 29 14 3 6]; % Create a 3x4 matrix.


>> A(3,1)=13 % Assign a new value to the (3,1) element.
A = 5 10 9 8
18 1 7 11
13 14 3 6
>> A(2,1) = A(2,1)-A(2,4) % Use elements in a mathematical expression.
A = 5 10 9 8
7 1 7 11
13 14 3 6
Using a Colon (:)
• For a vector
– va(:) – Refers to all the elements of the vector va.
– va(m:n) – Refer to element m through n of the
vector va.

>> v=[10 20 30 40 50]; >> v=[10 20 30 40 50];


>> v(3) >> x=v(2:end)
ans = 30 x = 20 30 40 50
>> w=v(2:4) >> y=v(2: length(v))
w= y = 20 30 40 50
20 30 40 >> v(3:end)=0
>> length(v) v=
ans = 5 10 20 0 0 0
Using a Colon (:)
• For a matrix
– A(:,n) – Refers to the elements in all the rows of column
n of the matrix A.

– A(n,:) – Refers to the elements in all the columns of row


n of the matrix A.

– A(:,m:n) – Refers to the elements in all the rows


between column m and n of the matrix A.

– A(m:n,:) – Refers to the elements in all the columns


between rows m and n of the matrix A.

– A(m:n,p:q) Refers to the elements in rows m through n


and columns p through q of the matrix A.
Using a Colon (:)
>> A =[2 4 13 9;16 3 7 11;8 21 6 5];
 2 4 13 9  >> C = A(2:3, 1:3)
  C = 16 3 7
A = 16 3 7 11
8 21 6
 8 21 6 5 
  >> D=A(: , 2) >> E=A(1:2, :)
D= 4 E=
3 2 4 13 9
21 16 3 7 11
Using a Colon (:)
Transformation of a matrix to a vector
>> B=[ 9:-2:1; zeros(1, 3) 1 2; ones(2, 5)*2 ]
B=
>> D=[11 12 13;21 22 23]
9 7 5 3 1
D=
0 0 0 1 2
11 12 13
2 2 2 2 2
21 22 23
2 2 2 2 2
>> v =D(:)
>> B(:, end) =1
v=
B=
11
9 7 5 3 1
21
0 0 0 1 1 11 12 13 
12 D=
2 2 2 2 1 
22  21 22 23
2 2 2 2 1
13
>> C= B( [1 3], [1, 3:4] ) 23
C= >> v=v’
9 5 3 v=
2 2 2 11 21 12 22 13 23
Adding Elements to Existing
Variables
• Adding elements to a vector
>> DF=1:4 % Define vector DF with 4 elements.
DF =
1 2 3 4
>> DF(5:10)=10:5:35 % Adding 6 elements starting with the 5th.
DF =
1 2 3 4 10 15 20 25 30 35
>> AD=[5 7 2] % Define vector AD with 3elements.
AD =
5 7 2
>> AD(8)=4 % Assign a value to the 8th element.
AD =
5 7 2 0 0 0 0 4
>> v(4)=5 % MATLAB assigns zeros to the 1st through
4th elements
v=0 0 0 0 5
Appending Existing Vectors
>> RE=[3 8 1 24];
>> GT=4:3:16;
>> KNH = [RE GT] % Define a new vector KNH by appending RE and GT.
KNH =
3 8 1 24 4 7 10 13 16
>> KNV=[RE’ ; GT’] % Create a new column vector KNV by appending RE’
and GT’.
KNV=
3
8
1
24
4
7
10
13
16
Adding Elements to a Matrix
• The size of the added rows and columns must fit the existing matrix.

>> E=[1 2 3 4; 5 6 7 8];


E= 1 2 3 4
5 6 7 8 Add the vecotr [10 14 18
>> E(3, : )=[10:4:22] 22] as the third row of E.
E= 1 2 3 4
5 6 7 8
10 14 18 22
>> E(3, 4)=1
>> K=eye(3);
E= 1 2 3 4
K= 1 0 0
5 6 7 8
0 1 0
0 0 0 1
0 0 1
>> G=[E K] % Append matrix K to E. >> H(3,4)=8
G= H=
1 2 3 4 1 0 0 0 0 0 0
5 6 7 8 0 1 0 0 0 0 0
10 14 18 22 0 0 1 0 0 0 8
Adding elements to a Matrix
>> AW=[3 6 9; 8 5 11]
AW =
3 6 9
8 5 11
>> AW(4,5) = 17
AW =
3 6 9 0 0
8 5 11 0 0
0 0 0 0 0
0 0 0 0 17
>> BG=(3,4)=15
BG =
0 0 0 0
0 0 0 0
0 0 0 15
Deleting Elements []
Vector
Matrix

>> kt=[2 8 40 65 3 55 23 15 75 80]


kt = >> A=[5 11 4 9;
2 8 40 65 3 55 23 15 75 80 4 0 26 10;
>> kt(6)=[] 56 1 5 89]
kt = A=
2 8 40 65 3 23 15 75 80 5 11 4 9
4 0 26 10
>> kt(3:6)=[]
56 1 5 89
kt =
>> A(:, 2:3) = []
2 8 15 75 80
A=
5 9
4 10
56 89
Built-In Functions for Handling
Arrays
• Length(A):
– Returns the number of elements in the Vector A
>>A=[5 9 2 4];
>> length(A)
ans =
4

• Size(A)
– Returns a row vector [m, n], where m and n are the size mxn of the
array A.
>> A=[6 1 4 0 12; 5 19 6 8 2]
A=
6 1 4 0 12
5 19 6 8 2
>> size (A)
ans =
2 5
Built-In Functions for Handling
Arrays
• reshape(A,m,n)
– Creates a m by n matrix from the elements of matrix A.
– The elements are taken column after column.
– Matrix A must have m times n elements.
>>A=[5 1 6; 8 0 2];
A=
5 1 6
8 0 2
>> B=reshape(A,3,2)
B=
5 0
8 6
1 2

• diag(v)
– When v is a vector, creates a square matrix with the elements of v in the diagonal.
>> v=[7 4 2];
>> A=diag(v)
A=
7 0 0
0 4 0
0 0 2
Built-In Functions for Handling
Arrays
• diag(A)
– When A is a matrix, creates a vector from the diagonal elements of A.
>>A=[1 2 3; 4 5 6; 7 8 9]
A=
1 2 3
4 5 6
7 8 9
>> vec=diag(A)
vec =
1
5
9
Create a Matrix
• Using the ones and zeros commands, create a 4x5
matrix in which the first two rows are 0s and the
next two rows are 1s.

You might also like