In: Electrical Engineering
Assume that A and B are MATLAB arrays with 10 rows and an equal number of columns (the number of columns is not given). Important: Next, the expression "a single line of code" implies a single command or equality. In other words, the code:
X=A+1; X=X+B; is considered to be TWO lines of code, even though it can be written as one line.
(a) (3%) Write a single line of code which saves the first two rows of array A as a new array X.
(b) (4%) Write a single line of code which replaces rows 4 and 5 of array A with rows 2 and 6 of array B.
(c) (3%) Write a single line of code which divides all elements in array A by the corresponding elements in array B and saves the resulted array as X.
(d) (4%) Write a single line of code which adds the transpose of array A to array B and saves the result in array X. What should the number of columns of A and B be so that this operation does not result in an error?
(e) (4%) Write a single line of code which uses the MATLAB function "sum()" to find the sum of each row
(f) (3%) Lets call N the unknown number of columns of arrays A and B. What is the size of array X = [A B]; ?
(g) (3%) Again, N is the number of columns of arrays A and B. What is the size of array X = [A;B]; ?
(h) (3%) Assume that the arrays A and B have more than 5 columns. Write a single line of code that divides all elements in A by the B element located at row 3 and column 4.
(i) (5%) Write a single line of code using the MATLAB function "find()" which finds the location of all elements in A which are greater than 1. Then, write another line of code which replaces all these elements in A by the number 2.
(i)
A = magic(10);
B = A';
X = A(1:2,:);
output:
>> A
A =
92 99 1 8 15 67 74 51 58 40
98 80 7 14 16 73 55 57 64 41
4 81 88 20 22 54 56 63 70 47
85 87 19 21 3 60 62 69 71 28
86 93 25 2 9 61 68 75 52 34
17 24 76 83 90 42 49 26 33 65
23 5 82 89 91 48 30 32 39 66
79 6 13 95 97 29 31 38 45 72
10 12 94 96 78 35 37 44 46 53
11 18 100 77 84 36 43 50 27 59
>> B
B =
92 98 4 85 86 17 23 79 10 11
99 80 81 87 93 24 5 6 12 18
1 7 88 19 25 76 82 13 94 100
8 14 20 21 2 83 89 95 96 77
15 16 22 3 9 90 91 97 78 84
67 73 54 60 61 42 48 29 35 36
74 55 56 62 68 49 30 31 37 43
51 57 63 69 75 26 32 38 44 50
58 64 70 71 52 33 39 45 46 27
40 41 47 28 34 65 66 72 53 59
>> X
X =
92 99 1 8 15 67 74 51 58 40
98 80 7 14 16 73 55 57 64 41
(ii)
A([4 5],:) = B([2 6],:);
after modification A is
A =
92 99 1 8 15 67 74 51 58 40
98 80 7 14 16 73 55 57 64 41
4 81 88 20 22 54 56 63 70 47
99 80 81 87 93 24 5 6 12 18
67 73 54 60 61 42 48 29 35 36
17 24 76 83 90 42 49 26 33 65
23 5 82 89 91 48 30 32 39 66
79 6 13 95 97 29 31 38 45 72
10 12 94 96 78 35 37 44 46 53
11 18 100 77 84 36 43 50 27 59
(iii)
X = A./B;
resultant X is
1.000000 1.010204 0.250000 0.094118 0.174419 3.941176
0.989899 1.000000 0.086420 0.160920 0.172043 3.041667
4.000000 11.571429 1.000000 1.052632 0.880000 0.710526
10.625000 6.214286 0.950000 1.000000 1.500000 0.722892
5.733333 5.812500 1.136364 0.666667 1.000000 0.677778
0.253731 0.328767 1.407407 1.383333 1.475410 1.000000
0.310811 0.090909 1.464286 1.435484 1.338235 0.979592
1.549020 0.105263 0.206349 1.376812 1.293333 1.115385
0.172414 0.187500 1.342857 1.352113 1.500000 1.060606
0.275000 0.439024 2.127660 2.750000 2.470588 0.553846
Columns 7 through 10:
3.217391 0.645570 5.800000 3.636364
11.000000 9.500000 5.333333 2.277778
0.682927 4.846154 0.744681 0.470000
0.696629 0.726316 0.739583 0.363636
0.747253 0.773196 0.666667 0.404762
1.020833 0.896552 0.942857 1.805556
1.000000 1.032258 1.054054 1.534884
0.968750 1.000000 1.022727 1.440000
0.948718 0.977778 1.000000 1.962963
0.651515 0.694444 0.509434 1.000000
(iv)
X = A'+B; %A' denotes transpose
X =
184 196 8 170 172 34 46 158 20 22
198 160 162 174 186 48 10 12 24 36
2 14 176 38 50 152 164 26 188 200
16 28 40 42 4 166 178 190 192 154
30 32 44 6 18 180 182 194 156 168
134 146 108 120 122 84 96 58 70 72
148 110 112 124 136 98 60 62 74 86
102 114 126 138 150 52 64 76 88 100
116 128 140 142 104 66 78 90 92 54
80 82 94 56 68 130 132 144 106 118
number of columns should be 10 as it is to be square