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

Creating Arrays

The document discusses creating arrays in MATLAB. It explains how to create vectors, matrices, and strings. It covers one-dimensional and two-dimensional arrays. Methods for creating vectors include specifying elements, linear spacing, and the transpose operator. Matrices use semicolons to separate rows. Common functions like zeros, ones, and eye are demonstrated.

Uploaded by

MṜ ΛßßΛS
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Creating Arrays

The document discusses creating arrays in MATLAB. It explains how to create vectors, matrices, and strings. It covers one-dimensional and two-dimensional arrays. Methods for creating vectors include specifying elements, linear spacing, and the transpose operator. Matrices use semicolons to separate rows. Common functions like zeros, ones, and eye are demonstrated.

Uploaded by

MṜ ΛßßΛS
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

Creating Arrays in MATLAB

Submitted by
Dr. Saib A. Yousif
Arrays
• An array is a list of numbers arranged in rows
and/or columns. The simplest array (one-
dimensional) is a row or a column of numbers
representing a vector. A more complex array (
two-dimensional) is a collection of numbers
arranged in rows and columns representing a
matrix.

2
Creating a vector
• The position of point A can be expressed in terms of a position vector: 𝑟𝐴 =
2i + 4j +5k where i, j, and k are unit vectors in the direction of the x, y, and
z axes, respectively. The numbers 2, 4, and 5 can be used to define a row
or a column vector.

3
• The vector is created by typing the elements (numbers)
inside square brackets [ ].
• variable_name = [ type vector elements ]
• Row vector: To create a row vector type the elements
with a space or a comma between the elements inside
the square brackets.
• Column vector: To create a 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.
4
Example
• Arrange the years in the following table as a row
vector and the population as a column vector.
Year 1984 1986 1988 1990 1992 1994 1996
Populations
127 130 136 145 158 178 211

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


yr =
1984 1986 1988 1990 1992 1994 1996

5
>> pop=[127; 130; 136; 145; 158; 178; 211]
pop =
127
130
136
145
158
178
211
>> pntAH=[2, 4, 5]
pntAH =
245
>> pntAV=[2
4
5]
pntAV =
2
4
5
>>

6
Creating a vector with constant spacing by specifying
the first term, the spacing, and the last term:

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


>> x=[1:2:13]
x=
1 3 5 7 9 11 13 1st element Spacing Last element
>> y=[1.5:0.1:2.1]
y=
1.5000 1.6000 1.7000 1.8000 1.9000 2.0000 2.1000
>> z=[-3:7] (spacing is 1)
z=
-3 -2 -1 0 1 2 3 4 5 6 7
>> xa=[21:-3:6]
xa =
21 18 15 12 9 6
>>

7
Notes
• If the numbers m, q, and n are such that the value of n cannot be obtained
by adding q’s to m, then (for positive n) the last element in the vector will
be the last number that does not exceed n.
>>X=[1:2:10]
X=
13579

• If only two numbers (the first and the last terms) are typed (the spacing is
omitted), then the default for the spacing is 1.
>>X=[1:10]
X=
1 2 3 4 5 6 7 8 9 10

8
Creating a vector with linear (equal) spacing by
specifying the first, last terms, and the
number of terms:

variable_name = linspace(xi,xf,n)

1st element Last element


No. of elements

q= (xf-xi)/(n-1) spacing

9
Examples
𝟖−𝟎
>> va=linspace(0,8,6) 𝐬𝐩𝐚𝐜𝐢𝐧𝐠 = 𝟔−𝟏 = 𝟏. 𝟔
va =
0 1.6000 3.2000 4.8000 6.4000 8.0000
>> vb=linspace(30,10,11) 𝒔𝒑𝒂𝒄𝒊𝒏𝒈 =(10-30)/(11-1)= - 2
vb =
30 28 26 24 22 20 18 16 14 12 10
𝟎.𝟓−𝟒𝟗.𝟓
>> u=linspace(49.5,0.5) 𝒔𝒑𝒂𝒄𝒊𝒏𝒈 = = − 𝟎. 𝟒𝟗𝟒𝟗 n=100
𝟏𝟎𝟎−𝟏
u=
Columns 1 through 10
49.5000 49.0051 48.5101 48.0152 47.5202 47.0253
46.5303 46.0354 45.5404 45.0455
............
Columns 91 through 100
4.9545 4.4596 3.9646 3.4697 2.9747 2.4798
1.9848 1.4899 0.9949 0.5000

10
CREATING A TWO-DIMENSIONAL ARRAY (MATRIX)

• Matrices used in science and engineering to describe


many physical quantities.
In a square matrix the number of rows and the number of
columns is equal.
For example, the matrix:
7 49
381
653

The above matrix is 3 x 3 matrix which is a square matrix,


with three rows and three columns.

11
• In general, the number of rows and columns can
be different. For example:

31 26 14 18 5 30
3 51 20 11 43 65 4 x 6 matrix
28 6 15 61 34 22
14 58 6 36 93 7
The above matrix has four rows and six columns. A
m x n matrix has m rows and n columns, and m by n
is called the size of the matrix.

12
• A matrix is created as followed:
variable_name=[1st row elements; 2nd row
elements; 3rd row elements; ... ; last row
elements]
Where the elements of the row are separated by
comma or space.

13
Examples
>> a=[5 35 43; 4 76 81; 21 32 40]
a =
5 35 43 A semicolon is typed before
4 76 81 a new line is entered.
21 32 40
>> b = [7 2 76 33 8
1 98 6 25 6
5 54 68 9 0] before a new line is entered.

b=
7 2 76 33 8
1 98 6 25 6
5 54 68 9 0
>> cd=6; e=3; h=4;
>> Mat=[e, cd*h, cos(pi/3); h^2,
sqrt(h*h/cd), 14]
Mat =
3.0000 24.0000 0.5000
16.0000 1.6330 14.0000

14
>> A=[1:2:11; 0:5:25; linspace(10,60,6); 67 2 43 68 4 13]
A =
1 3 5 7 9 11
0 5 10 15 20 25
10 20 30 40 50 60
67 2 43 68 4 13
>>

15
The zeros, ones and, eye Commands
>> zr=zeros(3,4)
zr =
0 0 0 0
0 0 0 0
0 0 0 0
>> ne=ones(4,3)

ne =
1 1 1
1 1 1
1 1 1
1 1 1
>> idn=eye(5)
idn =
1 0 0 0 0
0 1 0 0 0
0 0 1 0 0
0 0 0 1 0
0 0 0 0 1
>>

16
THE TRANSPOSE OPERATOR

Define a row vector aa


>> aa=[3 8 1]
aa =
3 8 1
Define a column vector bb as the transpose of vector aa.

>> bb=aa'
bb=
3
8
1

17
>> C=[2 55 14 8; 21 5 32 11; 41 64 9 1]
C =
2 55 14 8
21 5 32 11
41 64 9 1

>> D=C'
D=
2 21 41
55 5 64
14 32 9
8 11 1
>>

18
STRINGS
• A string is an array of characters.
• Strings can include letters, digits, other
symbols, and spaces.
• Examples of strings: 'ad ef ', '3%fr2',
'{edcba:21!', 'MATLAB'.
>> B='My name is John Smith'
B=
My name is John Smith
19
built-in function named char

variable_name = char('string 1','string 2','string 3')


Example:
>> Info=char('Student Name:','John Smith','Grade:','A+')
Info =
Student Name:
John Smith
Grade:
A+
>>

20

You might also like