Exercise 3: Working on Arrays
Exercise 3: Working on Arrays (Answer)
1.
2.
3.
4.
5.
6.
7.
No
The size is 0x0
Yes, the increment must be negative when the start value is greater than the end value.
The command is 0:%pi/16:2*%pi
a= diag(x) creates a vector a containing the diagonal elements of matrix x.
b=eye(a) .* a
b = diag([1:5]) creates a 5x5 matrix whose diagonal elements are the elements of the
vector
[1 2 3 4 5]).
diag([1:5])
ans =
1.
0.
0.
0.
0.
8.
0.
2.
0.
0.
0.
0.
0.
3.
0.
0.
0.
0.
0.
4.
0.
0.
0.
0.
0.
5.
diag([1:4], 1) creates a 5x5 matrix of zeros and puts the elements of the vector [1 2 3 4] on the
diagonal above the main diagonal. To place the vector on the diagonal below the main
diagonal
use b = diag([1:4], -1).
b = diag([1:4], 1) above main diagonal
b =
0. 1. 0. 0. 0.
0. 0. 2. 0. 0.
0. 0. 0. 3. 0.
0. 0. 0. 0. 4.
0. 0. 0. 0. 0.
b = diag([1:4], -1)
b =
0. 0. 0. 0.
1. 0. 0. 0.
0. 2. 0. 0.
0. 0. 3. 0.
0. 0. 0. 4.
9.
below main diagonal
0.
0.
0.
0.
0.
Type b = diag([1:4], -1)
b =
10.
0. 0. 0. 0. 0.
1. 0. 0. 0. 0.
0. 2. 0. 0. 0.
0. 0. 3. 0. 0.
0. 0. 0. 4. 0.
Type b=diag([1:5]) + diag([6:9], 1) + diag([10:13], -1)
b=diag([1:5]) + diag([6:9], 1) + diag([10:13], -1)
b =
1.
10.
0.
0.
0.
11.
6.
0. 0.
2. 7. 0.
11. 3. 8.
0. 12. 4.
0. 0. 13.
0.
0.
0.
9.
5
-->a=[1 2 3;4 5 6;7 8 9]
a =
1.
4.
7.
2.
5.
8.
3.
6.
9.
-->b=a'
b =
1.
2.
3.
4.
5.
6.
7.
8.
9.
-->c=a+b
c =
2. 6. 10.
6. 10. 14.
10. 14. 18.
-->d=a-b
d =
0. - 2. - 4.
2. 0. - 2.
4. 2. 0.
-->e=a*b
e =
14.
32.
32.
77.
50.
122.
50.
122.
194.
-->f = [3 1 2;1 5 3;2 3 6]
f =
3.
1.
2.
1.
5.
3.
2.
3.
6.
-->g=inv(f)
g =
0.4285714 0.
- 0.1428571
0.
0.2857143 - 0.1428571
- 0.1428571 - 0.1428571 0.2857143
-->f*g
ans =
1.
0.
0.
0.
1.
0.
0.
1.110D-16
1.
-->det(f)
ans =
49.
-->log(a)
ans =
0.
0.6931472 1.0986123
1.3862944 1.6094379 1.7917595
1.9459101 2.0794415 2.1972246
-->a.*b
ans =
1. 8. 21.
8. 25. 48.
21. 48. 81.
-->a^2
ans =
30. 36. 42.
66. 81. 96.
102. 126. 150.
-->a.^2
ans =
12.
1. 4. 9.
16. 25. 36.
49. 64. 81.
-->a=zeros(5,8)
a =
0.
0.
0.
0.
0.
0.
0.
0.
0.
0.
0.
0.
0.
0.
0.
0.
0.
0.
0.
0.
0.
0.
0.
0.
0.
0.
0.
0.
0.
0.
1.
1.
1.
1.
1.
1.
1.
1.
1.
1.
1.
1.
-->b=ones(4,6)
b =
1.
1.
1.
1.
1.
1.
1.
1.
1.
1.
1.
1.
-->c=eye(3,3)
c =
1.
0.
0.
0.
1.
0.
0.
0.
1.
-->d=eye(3,3)*10
d =
10. 0. 0.
0. 10. 0.
0. 0. 10.
0.
0.
0.
0.
0.
0.
0.
0.
0.
0.