0% found this document useful (0 votes)
6 views

ENGR113 - Lecture 4- Creating Arrays-A

The document provides an introduction to creating and manipulating arrays in MATLAB, covering one-dimensional arrays (vectors) and two-dimensional arrays (matrices). It explains how to create, access, and modify elements in arrays, as well as the use of commands like zeros, ones, and eye for matrix creation. Additionally, it discusses array addressing, including the use of the colon operator for selecting ranges of elements.

Uploaded by

ahenxk
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

ENGR113 - Lecture 4- Creating Arrays-A

The document provides an introduction to creating and manipulating arrays in MATLAB, covering one-dimensional arrays (vectors) and two-dimensional arrays (matrices). It explains how to create, access, and modify elements in arrays, as well as the use of commands like zeros, ones, and eye for matrix creation. Additionally, it discusses array addressing, including the use of the colon operator for selecting ranges of elements.

Uploaded by

ahenxk
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

Introduction to Computing

using Matlab

Creating Arrays-A
Arrays: Introduction
Generally speaking:
• An array is a systematic arrangement of objects, usually in rows
and columns ….
Wikipedia
Creating a One-Dimensional Array (Vector)
• An array is MATLAB's basic data structure
• Can have any number of dimensions. Most common are
• vector - one dimension (a single row or column)
• matrix - two or more dimensions
• Arrays can have numbers or letters
• To create a row vector from known numbers, type variable name, then equal sign,
then inside square brackets, numbers separated by spaces and/or commas
variable_name = [ n1, n2, n3 ]

>> yr = [1984 1986 1988 1990 1992 1994 1996]


yr =
1984 1986 1988 1990 1992 1994 1996 Note MATLAB displays row vector horizontally
To create a column vector from known numbers put semicolon
after all but last number
variable_name = [ n1; n2; n3 ]

>> yr = [1984; 1986; 1988 ]


yr =
1984 Note MATLAB displays column vector vertically
1986
1988
To create a vector with specified constant spacing between elements
variable_name = m:q:n
• m is first number
• n is last number
• q is difference between consecutive numbers
v = m:q:n
means
v = [ m m+q m+2q m+3q ... n ]
If omit q, spacing is one
v = m:n
means
v = [ m m+1 m+2 m+3 ... n ]
>> x = 1:2:13
x = 1 3 5 7 9 11 13

>> y = 1.5:0.1:2.1 Non-integer spacing


y = 1.5000 1.6000 1.7000 1.8000 1.9000 2.0000 2.1000

>> z = -3:7
z = -3 -2 -1 0 1 2 3 4 5 6 7

>> xa = 21:-3:6 Negative spacing


xa = 21 18 15 12 9 6
To create a vector with specified number of terms between first and last
v = linspace( xi, xf, n )
• xi is first number
• xf is last number
• n is number of terms (= 100 if omitted)

>> va = linspace( 0, 8, 6 ) Six elements


va = 0 1.6000 3.2000 4.8000 6.4000 8.0000

>> va = linspace( 30, 10, 11 ) Decreasing elements


va=30 28 26 24 22 20 18 16 14 12 10
Creating a Two-Dimensional Array (Matrix)
Create a two-dimensional matrix like this
m = [ row 1 numbers; row 2 numbers; ... ; last row
numbers ]
• Each row separated by semicolon
• All rows have same number of columns

>> a=[ 5 35 43; 4 76 81; 21 32 40]


a =
5 35 43
4 76 81
21 32 40
What if number of columns different?
Four columns Five columns

>> B= [ 1:4; linspace(1,4,5) ]


??? Error using ==> vertcat
CAT arguments dimensions are not consistent.
The zeros, ones and, eye Commands
>> zr=zeros(3,4)
• zeros(m,n) - makes matrix of m
zr = 0 0 0 0
rows and n columns, all with zeros 0 0 0 0
0 0 0 0

• ones(m,n) - makes matrix of m rows >> ne=ones(4,3)

and n columns, all with ones ne = 1 1 1


1 1 1
1 1 1
1 1 1
• eye(n) - makes square matrix of n >> idn=eye(4)
rows and columns. Main diagonal idn = 1 0 0 0
(upper left to lower right) has ones, all 0 1 0 0
other elements are zero 0 0 1 0
0 0 0 1
• To make a matrix filled with a particular number, multiply
ones(m,n) by that number

>> z=100*ones(3,4)
z =
100 100 100 100
100 100 100 100
100 100 100 100
The Transpose Operator
Transpose a variable by putting a single quote after it, e.g., x'
• In math, transpose usually denoted by superscript "T", e.g., xT
• Converts a row vector to a column vector and vice-versa
• Switches rows and columns of a matrix, i.e., first row of original becomes first
column of transposed, second row of original becomes second column of
transposed, etc.

>> aa=[3 8 1]
aa = 3 8 1
>> bb=aa'
bb = 3
8
1
Array Addressing
Can access (read from or write to) elements in array (vector or matrix)
individually or in groups
• Useful for changing subset of elements
• Useful for making new variable from subset of elements
Address of element is its position in the vector
• "address" often called index
• Addresses always start at 1 (not 0)
• Address 1 of row vector is leftmost element
• Address 1 of column vector is topmost element
• To access element of a vector represented by a variable, follow variables name by
address inside parentheses, e.g., v(2)=20 sets second element of vector v to 20
Array Addressing: Vector
>> VCT=[35 46 78 23 5 14 81 3 55]
VCT = 35 46 78 23 5 14 81 3 5
>> VCT(4)
ans = 23
>> VCT(6)=273
VCT = 35 46 78 23 5 273 81 3 5
>> VCT(2)+VCT(8)
ans = 49
>> VCT(5)^VCT(8)+sqrt(VCT(7))
ans = 134
Array Addressing: Matrix
Address of element in a matrix is given by row number and column number.
Address often called index or subscript
• Addresses always start at 1 (not 0)
• Row 1 is top row
• Column 1 is left column
• If variable ma is a matrix, ma(k,p) is element in row k and column p
>> MAT=[3 11 6 5; 4 7 10 2; 13 9 0 8]
Column 1
MAT = 3 11 6 5
Element in
row 3 and
4 7 10 2
column 1 13 9 0 8 Row 3
>> MAT(3,1)
ans = 13
Using A Colon : In Addressing Arrays

The colon : lets you address a range of elements


Vector (row or column)
• va(:) - all elements
• va(m:n) - elements m through n
Matrix
• A(:,n) - all rows of column n
• A(m,:) - all columns of row m
• A(:,m:n) - all rows of columns m through n
• A(m:n,:) - all columns of rows m through n
• A(m:n,p:q) - columns p through q of rows m through n
>> A=[1:2:11; 2:2:12; 3:3:18; 4:4:24; 5:5:30]
A = 1 3 5 7 9 11
2 4 6 8 10 12
3 6 9 12 15 18
4 8 12 16 20 24
5 10 15 20 25 30

>> C=A(2,:) All columns of row 2


C = 2 4 6 8 10 12

>> F=A(1:3,2:4) Columns two through four of rows one through three
F = 3 5 7
4 6 8
6 9 12
Can replace vector index or matrix indices by vectors in order to pick out
specific elements. For example,
m([a b],[c:d e]) returns columns c through d and column e of rows
a and b for matrix m
>> A=[10:-1:4; ones(1,7); 2:2:14; zeros(1,7)]
A = 10 9 8 7 6 5 4
1 1 1 1 1 1 1
2 4 6 8 10 12 14
0 0 0 0 0 0 0

>> B=A([1 3],[1 3 5:7])


B = 10 8 6 5 4
2 6 10 12 14
Adding Elements To Existing Variables

Assigning to undefined indices of vectors


>> DF=1:4
DF = 1 2 3 4
>> DF(5:10)=10:5:35
DF = 1 2 3 4 10 15 20 25 30 35
>> AD=[5 7 2]
AD = 5 7 2
>> AD(8)=4
AD = 5 7 2 0 0 0 0 4
>> AR(5)=24
Unassigned elements set to zero
AR = 0 0 0 0 24
Assigning to undefined indices of matrices
>> AW=[3 6 9; 8 5 11]
AW = 3 6 9
8 5 11
>> AW(4,5)=17 AW doesn't have a fourth row or fifth column
AW = 3 6 9 0 0 Now it does!
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 Unassigned elements set to zero
0 0 0 15
Deleting Elements
To delete elements in a vector or matrix, set range to be
deleted to empty brackets
>> mtr=[5 78 4 24 9; 4 0 36 60 12; 56 13 5 89 3]
mtr = 5 78 4 24 9
4 0 36 60 12
56 13 5 89 3
>> mtr(:,2:4)=[]
mtr = 5 9
4 12
56 3

You might also like