The following exercises are meant to be answered by few lines of MATLAB code; mo
ID: 3804606 • Letter: T
Question
The following exercises are meant to be answered by few lines of MATLAB code; most of them could be expressed in a single line of code or command. The command may be involved (i.e., it may use a number of parentheses or calls to functions) but can, in essence, be solved by the execution of a single command. If the problem is too complicated, feel free to break it up over two or more lines and later to collapse it into a single line.
4. Given the array A = [2 7 9 7 ; 3 1 5 6 ; 8 1 2 5], explain the results of the following commands:
a. A'
b. A(:,[1 4])
c. A([2 3],[3 1])
d. reshape(A,2,6)
e. A(:)
f. flipud(A)
g. fliplr(A)
h. [A A(end,:)]
i. A(1:3,:)
j. [A ; A(1:2,:)]
k. sum(A)
l. sum(A')
m. sum(A,2)
n. [ [ A ; sum(A) ] [ sum(A,2) ; sum(A(:)) ] ]
Explanation / Answer
>> A'
ans =
2 3 8
7 1 1
9 5 2
7 6 5
>> A(:,[1 4])
ans =
2 7
3 6
8 5
>> A([2 3],[3 1])
ans =
5 3
2 8
>> reshape(A,2,6)
ans =
2 8 1 9 2 6
3 7 1 5 7 5
>> A(:)
ans =
2
3
8
7
1
1
9
5
2
7
6
5
>> flipud(A)
ans =
8 1 2 5
3 1 5 6
2 7 9 7
fliplr(A)
ans =
7 9 7 2
6 5 1 3
5 2 1 8
>> A(1:3,:)
ans =
2 7 9 7
3 1 5 6
8 1 2 5
[A ; A(1:2,:)]
ans =
2 7 9 7
3 1 5 6
8 1 2 5
2 7 9 7
3 1 5 6
>> sum(A)
ans =
13 9 16 18
>> sum(A')
ans =
25 15 16
>> sum(A,2)
ans =
25
15
16
>> [ [ A ; sum(A) ] [ sum(A,2) ; sum(A(:)) ] ]
ans =
2 7 9 7 25
3 1 5 6 15
8 1 2 5 16
13 9 16 18 56
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.