Matlab Exercises 1
Matlab Exercises 1
11 Problems 55
>> X=536
X =
r
536
>> y='536'
la
y =
536
>>
The two variables are not the same even though they appear identical on the
screen. Note that the characters 536 in the line below the x= are indented, while
the characters 536 in the line below the y= are not indented. The variable x can be
te
used in mathematical expressions, whereas the variable y cannot.
2.11 PROBLEMS
1. Create a row vector that has the following elements: 8, 10/4 , 12 x 1.4, 5 1,
ln 35 /0.43 ,
sin65
cos80
:, 129, and cos2(1t/20) .
(l4tan5 8o)
3. Create a column vector that has the following elements: 25 5
"'(2.1 2+11) '
6!, 2.74, 0.0375, and 1t/S.
32
4. Create a column vector that has the following elements: , sin235°, 6.1,
3_2 2
ln292, 0.00552, In229, and 133.
5. Define the variables x = 0.85 , y = 12.5 , and then use them to create a col
umn vector that has the following elements: y , yx , In (yIx) , x x y , and
x+y.
6. Define the variables a = 3.5 , b = -6.4, and then use them to create a row
vector that has the following elements: a , a 2, a/b , a b, and · JQ. .
56 Chapter 2: Creatin.: Arrays
7. Create a row vector in which the ftrst element is 1 and the last element is 43,
with an increment of 6 between the elements (1, 7, 13, . . . , 43 ).
8. Create a row vector with 11 equally spaced elements in which the ftrst ele
ment is96 and the last element is 2.
r
9. Create a column vector in which the ftrst element is 26, the elements decrease
with increments of -3. 6, and the last element is -10. (A column vector can be
la
created by the transpose of a row vector. )
10. Create a column vector with9 equally spaced elements in which the ftrst ele
ment is -34 and the last element is -7. (A column vector can be created by the
transpose of a row vector. )
11. Using the colon symbol, create a row vector (assign it to a variable named
te
Fives) with ftve elements that are all 5.
12. Using the linspace command, create a row vector (assign it to a variable
named Nines) with nine elements that are all9.
13. Use a single command to create a row vector (assign it to a variable named a)
with 6 elements such that the last element is 4. 7 and the rest of the elements
Es
are Os. Do not type the vector elements explicitly.
14. Use a single command to create a row vector (assign it to a variable named b)
with 8 elements such that the last three element are 3. 8 and the rest of the ele
ments are Os. Do not type the vector elements explicitly.
15. Use a single command to create a row vector (assign it to a variable named b)
with 11 elements such that
b =
0 2 4 6 8 10 12 9 6 3 0
Do not type the vector explicitly.
16. Create two row vectors: a=2: 3:17 andb=3:4:15. Then, by only using the
name of the vectors (a and b), create a row vector c that is made from the ele
ments of a followed by the elements of b.
only using the name of the vectors (a and b), create a column vector c that is
made from the elements of a followed by the elements of b.
2.11 Problems 57
18. Create a vector (name it vtA) that has 10 elements of which the ftrst is 8, the
increment is 7, and the last element is 71. Then, assign elements ofvtA to a
new vector (call it vtB) vtB has 7 elements. The ftrst 4 elements
such that
are the ftrst 4 elements of the vector vtA,and the last 3 are the last 3 elements
of the vector vtA. Do not type the elements ofvtA vector explicitly.
r
19. Create a vector (name it vetC) that has 12 elements of which the ftrst is 5, the
increment is 4 and the last element is 49. Then, by assigning elements of
la
vctC to new vectors, create the following two vectors:
(a) A vector (name it Codd) that contains all the elements with odd index of
vctC; i.e., Codd 5 13 21 ... 45 .
=
(b) A vector (name it Ceven) that contains all the elements with even index
ofvct;i.�,Ceven 9 17 25 ... 49.
=
In both parts use vectors of odd and even numbers for the index of Codd and
Ceven,respectively. Do not type the elements of the vectors explicitly.
te
20. Create a vector (name it vctD) that has 9 elements of which the ftrst is 0, the
increment is 3 and the last element is 27. Then create a vector (name it vet
Dop) that consist of the elements ofvctD in reverse order. Do it by assigning
elements ofvctD to vctDop. (Do not type the elements ofvctDop vector
explicitly.)
Es
21. Create the following matrix by using vector notation for creating vectors with
constant spacing and/or the linspace command. Do not type individual
elements explicitly.
A =
[130
1
110 90 70
2.8 333 4.66676.5 8.3333 10.1
50
667 12
30 10 j
12 2 2 32 42 52 6 2 72
22. Create the following matrix by using vector notation for creating vectors with
the linspace command. Do not type individual elements explicitly.
r1
5 2 3
5 2 3
B=
5 2 3
5 2 3
23. Create the following matrix by typing one command. Do not type individual
elements explicitly.
C= [
77777
77777
l
j
58 Chapter 2: Creatin.: Arrays
24. Create the following matrix by typing one command. Do not type individual
elements explicitly.
[00008l
D= 00007
00006
r
25. Create the following matrix by typing one command. Do not type individual
elements explicitly.
la
E=
roooool
00000
005 4 3
002 1 0
26. Create the following matrix by typing one command. Do not type individual
te
elements explicitly.
F=
rooo o ol
001 1020
002 8 26
003 6 32
Es
27. Create three row vectors:
a = [3 -1 5 11 -4 2] , b= [7 -9 2 13 1 -2] ,
-78 09] c = [-2 4
(a) Use the three vectors in a MATLAB command to create a 3 x 6 matrix in
which the rows are the vectors a, b, c, and c.
second row consists of elements 4 through 7 of vector a, and the third row
consists of elements 2 through 5 of vector b.
(b) Use the two vectors in a MATLAB command to create a 6x2 matrix
such that the first column consists of elements 2 through 7 of vector a,
r
of vector b.
30. By hand (pencil and paper) write what will be displayed if the following com
la
mands are executed by MATLAB. Check your answers by executing the com
mands with MATLAB. (Parts (b), (c), (d), and (e) use the vector that was
defmed in part
(a).)
(a) a=1: 4: 17 (b)b= a
[ (1: 3)a] (c) c= [a;a] '
A =
[
36 34 32 30 28 26
24 22 20 18 16 14
j
12 10 8 6 4 2
By writing one command and using the colon to address range of elements
(do not type individual elements explicitly), use the matrix A to:
(a) Create a six-element row vector named ha that contains the elements of
the second row of A.
(b) Create a three-element column vector named hb that contains the ele
ments of the sixth column of A.
(c) Create a five-element row vector named he that contains the first two ele
ments of the third row of A and the last three element of the first row of A.
60 Chapter 2: Creatin.: Arrays
[ 45
1 7 10 13 16 j
r
B = 2 8 11 14 17
3 6 9 12 15 18
la
By writing one command and using the colon to address range of elements
(do not type individual elements explicitly), use the matrix B to:
(a) Create a nine-element column vector named Ba that contains the ele
ments of the ftrst, third, and fifth columns of B.
(b) Create a seven-element row vector named Bb that contains elements 2
through 5 of the second row of Band the elements of the third column of B.
(c) Create a six-element row vector named Be that contains elements 3
te through 5 of the first row, and elements 2 through 4 of the third row of B.
(a) Create a eight-element column vector named Da that contains the ele
ments of the first and third rows of D.
(b) Create an eight-element raw vector named Db that contains the elements
of the second and the fourth columns of D.
(c) Create a eight-element row vector named De that contains the first two
elements of the first row, the last three elements of the second column, and
the first three elements of the fourth row of D.
E =
ro 5 5 5 5 5]
0.1 0.3 0.5 0.7 0.7 0.9
12 9 6 3 0 -3
6 7 8 9 10 11
(a) Create a 2x3 matrix F from the second and third rows, and the third
2.11 Problems 61
r
H=
[1.25 1.5 1.75 2 2.25 2.5 2.75
1 2 3 1 2 3 4
1
la
45 40 35 30 25 20 15
(a) Create a 2 x 5 matrix G such that its first row includes the first three ele
ments and the last two elements of the first row of H, and the second row
of G includes the last five elements of the third row of H.
(b) Create a 4 x 3 matrix K such that the first, second, third, and fourth rows
are the second, third, fifth and seventh columns of matrix H.
te
38. The following matrix is defined in MATLAB:
A{=
[
1 4 7 10 13 16
2 5 8 11 14 17
j
3 6 9 12 15 18
By hand (pencil and paper) write what will be displayed if the following com
Es
mands are executed by MATLAB. Check your answers by executing the com
mands with MATLAB.
a) A=M([113]I [11516]) b) B=M(:1[414:6])
c) C=M([ 11 2]I:) d) D=M( [ 2I3] I[ 2I3] )
N
=
r � :�J
6 14 23 35 47
�� �� ��
8 16 26 38 50
By hand (pencil and paper) write what will be displayed if the following com
mands are executed by MATLAB. Check your answers by executing the com
mands with MATLAB.
(a) A=[N(11 1: 4) 1 IN(2I 2:5) 1 ]
(b) B=[N(:I3) I N(31:) ]
(c) C (3:415:6) =N( 2:314: 5)
40. By hand (pencil and paper) write what will be displayed if the following com
mands are executed by MATLAB. Check your answers by executing the com
mands with MATLAB.
62 Chapter 2: Creatin.: Arrays
V=1:2:23
M=reshape(v,3,4)
M(2,:)=[]
M(:,3)=[]
N=ones(size(M))
r
41. Using the zeros, ones, and eye commands, create the following arrays by
typing one command:
la
(a)
III
[I o ol III IIII]
0 0 0
�� � � �l
l� �J
(b) (c)
o oj lo o o 0 0
42. Using the zeros, ones, and eye commands create the following arrays by
typing one command:
te riIIIiiilII rOIIIOl
(a) o
IIIIIII ol (b) (c) 0IIIII 0
lo oj 0 0 0 0 0 0 0
0I 0 0
43. Use the eye, ones, and zeros commands to create the following arrays:
0 0 0 0
Es
A= [� �] B= [� �] C= [� �]
Using the variables A, B, and C, write a command that creates the following
matrix D:
D
=
lOIIIOO
iOIIOOl
0 0 IIIIII 0
0 0 0
44. Create a 2 x 3 matrix A in which all the elements are 1. Then reassign A to
itself(several times) such that A will become:
IIIIII 0 0
0 0
0 0
A=
0 0
0 0
0 0
IIIIII