Chapter 2: Creating Arrays 2.11 Problems
Chapter 2: Creating Arrays 2.11 Problems
Chapter 2: Creating Arrays 2.11 Problems
11 Problems 57
8. Create a row vector with 9 equally spaced elements in which the first element 16. Create the following matrix by using vector notation for creating vectors with
is 81 and the last element is 12. constant spacing and/or the linspace command. Do not type individual
elements explicitly.
9. Create a column vector in which the first element is 22.5, the elements
0 5 10 15 20 25 30
decrease with increments of –2.5, and the last element is 0. (A column vector A = 600 500 400 300 200 100 0
can be created by the transpose of a row vector.)
0 0.8333 1.6667 2.5 3.3333 4.1667 5
10. Create a column vector with 15 equally spaced elements in which the first ele-
ment is –21 and the last element is 12. 17. Create the following matrix by using vector notation for creating vectors with
constant spacing and/or the linspace command. Do not type individual
11. Using the colon symbol, create a row vector (assign it to a variable named elements explicitly.
same) with seven elements that are all –3. 1 0 3
2 0 3
12. Use a single command to create a row vector (assign it to a variable named a) B = 3 0 3
with 9 elements such that the last element is 7.5 and the rest of the elements
4 0 3
are 0s. Do not type the vector explicitly.
5 0 3
13. Use a single command to create a row vector (assign it to a variable named b)
with 19 elements such that 18. Using the colon symbol, create a 4 u 6 matrix (assign it to a variable named
b = 1 2 3 4 5 6 7 8 9 10 9 8 7 6 5 4 3 2 1 Anine) in which all the elements are the number 9.
Do not type the vector explicitly.
19. Create the following matrix by typing one command. Do not type individual
14. Create a vector (name it vecA) that has 14 elements of which the first is 49, elements explicitly.
the increment is –3, and the last element is 10. Then, using the colon symbol, 0 0 0 0 0
create a new vector (call it vecB) that has 8 elements. The first 4 elements are C = 0 0 0 0 0
the first 4 elements of the vector vecA, and the last 4 are the last 4 elements 0 0 0 0 8
of the vector vecA.
20. Create the following matrix by typing one command. Do not type individual
15. Create a vector (name it vecC) that has 16 elements of which the first is 13, elements explicitly.
the increment is 4 and the last element is 73. Then create the following two
vectors: 0 0 0 0 0
D = 0 0 0 6 6
(a) A vector (name it Codd) that contains all the elements with odd index of
vecCodd (vecCodd(1), vecCodd(3), etc; i.e., Codd = 13 21 29 ... 0 0 0 6 6
69).
(b) A vector (name it Ceven) that contains all the elements with even index 21. Create the following matrix by typing one command. Do not type individual
of vecCodd (vecCodd(2), vecCodd(4), etc; i.e., Codd = 17 25 33 elements explicitly.
... 73). 0 0 0 0 0
In both parts use vectors of odd and even numbers for the index of Codd and
E = 0 0 1 2 3
Ceven, respectively. Do not type the vectors explicitly.
0 0 4 5 6
0 0 7 8 9
58 Chapter 2: Creating Arrays 2.11 Problems 59
22. Create the following matrix by typing one command. Do not type individual 27. The following vector is defined in MATLAB:
elements explicitly. v = 15 0 6 – 2 3 – 5 4 9 1.8 – 0.35 7
0 0 0 0 0 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-
F = 0 0 1 10 20
0 0 2 8 26 mands with MATLAB.
0 0 3 6 32 (a) a=v(2:5) (b) b=v([1,3:7,11]) (c) c=v([10,2,9,4])
23. Create three row vectors: 28. The following vector is defined in MATLAB:
v = 15 0 6 – 2 3 – 5 4 9 1.8 – 0.35 7
a = 7 2 – 3 1 0 , b = – 3 10 0 7 – 2 , c = 1 0 4 – 6 5
(a) Use the three vectors in a MATLAB command to create a 3 u 5 matrix in By hand (pencil and paper) write what will be displayed if the following com-
which the rows are the vectors a, b, and c. mands are executed by MATLAB. Check your answers by executing the com-
(b) Use the three vectors in a MATLAB command to create a 5 u 3 matrix in mands with MATLAB.
which the columns are the vectors a, b, and c. (a) a=[v([2 7:10]);v([3,5:7,2])]
(b) b=[v([3:5,8])' v([10 6 4 1])' v(7:-1:4)']
24. Create three row vectors:
29. Create the following matrix A.
a = 7 2 – 3 1 0 , b = – 3 10 0 7 – 2 , c = 1 0 4 – 6 5
1 2 3 4 5 6
(a) Use the three vectors in a MATLAB command to create a 3 u 3 matrix
A = 7 8 9 10 11 12
such that the first, second, and third rows consist of the first three ele-
ments of the vectors a, b, and c, respectively. 13 14 15 16 17 18
(b) Use the three vectors in a MATLAB command to create a 3 u 3 matrix Use the matrix A to:
such that the first, second, and third columns consist of the last three ele- (a) Create a six-element row vector named ha that contains the elements of
ments of the vectors a, b, and c, respectively. the first row of A.
(b) Create a three-element row vector named hb that contains the elements of
25. Create two row vectors: the sixth column of A.
a = – 4 10 0.5 1.8 – 2.3 7 , b = 0.7 9 – 5 3 – 0.6 12 (c) Create a six-element row vector named hc that contains the first three
elements of the second row of A and the last three element of the third row
(a) Use the two vectors in a MATLAB command to create a 2 u 4 matrix
of A.
such that the first row consists of elements 2 through 5 of vector a, and the
second row consists of elements 3 through 6 of vector b.
30. Create the following matrix B.
(b) Use the two vectors in a MATLAB command to create a 3 u 4 matrix
such that the first column consists of elements 2 through 4 of vector a, the 18 17 16 15 14 13
second column consists of elements 4 through 6 of vector a, the third col- B = 12 11 10 9 8 7
umn consists of elements 1 through 3 of vector b, and the fourth column 6 5 4 3 2 1
consists of elements 3 through 5 of vector b. Use the matrix B to:
(a) Create a six-element column vector named va that contains the elements
26. By hand (pencil and paper) write what will be displayed if the following com-
of the second and fifth columns of B.
mands are executed by MATLAB. Check your answers by executing the com-
(b) Create a seven-element column vector named vb that contains elements 3
mands with MATLAB. (Parts (b), (c), and (d) use the vector that was defined
through 6 of the third row of B and the elements of the second column of B.
in part (a).)
(c) Create a nine-element column vector named vc that contains the ele-
(a) a=9:-3:0 (b) b=[a a] or b=[a,a] (c) c=[a;a]
ments of the second, fourth, and sixth columns of B.
(d) d=[a’ a’] or d=[a’,a’] (e) e=[[a; a; a; a] a’]
60 Chapter 2: Creating Arrays 2.11 Problems 61
31. Create the following vector C. 34. The following matrix is defined in MATLAB:
C = 0.7 1.9 3.1 4.3 5.5 6.7 7.9 9.1 10.3 11.5 12.7 13.9 15.1 16.3 17.5 3 5 7 9 11 13
Then use MATLAB’s built-in reshape function and the transpose operation M = 15 14 13 12 11 10
to create the following matrix D from the vector C: 1 2 3 1 2 3
0.7 1.9 3.1 4.3 5.5
D = 6.7 7.9 9.1 10.3 11.5 By hand (pencil and paper) write what will be displayed if the following com-
12.7 13.9 15.1 16.3 17.5 mands are executed by MATLAB. Check your answers by executing the com-
mands with MATLAB.
Use the matrix D to:
a) A=M([1,2],[2,4,5]) b) B=M(:,[1:3,6])
(a) Create a nine-element column vector named ua that contains the ele- c) C=M([1,3],:) d) D=M([2,3],5)
ments of the first, third, and fourth columns of D.
(b) Create an eight-element raw vector named ub that contains the elements 35. The following matrix is defined in MATLAB:
of the second row of D and the third column of D.
(c) Create a six-element row vector named uc that contains the first three ele- 33 21 9 14 30
ments of the first row of D and the last three elements of the last row of D. N = 30 18 6 18 34
27 15 6 22 38
32. Create the following matrix E. 24 12 10 26 42
0 0 0 0 2 2 2
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-
E = 0.7 0.6 0.5 0.4 0.3 0.2 0.1
mands with MATLAB.
2 4 6 8 10 12 14
(a) A=[N(1,1:4)’,N(2,2:5)’]
22 19 16 13 10 7 4 (b) B=[N(:,3)' N(3,:)]
(a) Create a 2 u 5 matrix F from the second and fourth rows, and the third (c) C(3:4,5:6)=N(2:3,4:5)
through the seventh columns of matrix E.
(b) Create a 4 u 3 matrix G from all rows and the third through fifth columns 36. By hand (pencil and paper) write what will be displayed if the following com-
of matrix E. mands are executed by MATLAB. Check your answers by executing the com-
mands with MATLAB.
33. Create the following matrix H. v=1:3:34
M=reshape(v,3,4)
1.7 1.6 1.5 1.4 1.3 1.2 M(2,:)=[]
H = 22 24 26 28 30 32 M(:,3)=[]
9 8 7 6 5 4 N=ones(size(M))
(a) Create a 2 u 4 matrix G such that its first row includes the first two ele-
ments and the last two elements of the first row of H, and the second row 37. Using the zeros, ones, and eye commands, create the following arrays:
of G includes the second through the fifth elements of the third row of H.
(b) Create a 3 u 3 matrix K such that the first, second, and third rows are the 1 1 1 1 1 1
first, fourth, and sixth columns of matrix H. 10 0 1 1 1
(a) 1 1 (b) (c) 1 1 1 1
01 0 1 1 1
0 0 0 0 0 0
00 1 1 1 1
0 0 1 1 1 1
62 Chapter 2: Creating Arrays
38. Using the zeros, ones, and eye commands create the following arrays:
0 0 1 1 1 1 0 0 1
(a) 1 0 0 11 (b) 0 0 1 1 (c) 1 1 0 0 0
0 1 0 11 0 0 0 0 1 1 0 0 0
1 1 1 1 1 1 0 0 0
39. Use the eye command to create the array A shown on the left below. Then use
the colon to address elements in the arrays and the eye command to change A
to match the array shown on the right.
1 0 0 0 0 0 1 0 0 1 0 0
0 1 0 0 0 0 0 1 0 0 1 0
A = 0 0 1 0 0 0 A = 0 0 1 0 0 1
0 0 0 1 0 0 1 0 0 1 0 0
0 0 0 0 1 0 0 1 0 0 1 0
0 0 0 0 0 1 0 0 1 0 0 1
40. Create a 2 u 2 matrix A in which all the elements are 1. Then reassign A to
itself (several times) such that A will become:
1 1 0 0 1 1 0 0
A = 1 1 0 0 1 1 0 0
0 0 1 1 0 0 1 1
0 0 1 1 0 0 1 1