(MATLAB) Enter the matrix A = [1 2 3 4; 5 6 7 8; 9 10 11 12] (i) Find two-elemen
ID: 2080913 • Letter: #
Question
(MATLAB) Enter the matrix A = [1 2 3 4; 5 6 7 8; 9 10 11 12]
(i) Find two-element arrays I and J such that A(I,J) is a 2 × 2 matrix consisting of the corner elements of A.
(ii) Suppose that B=A initially. Find two-element arrays K and L such that B(:,K) = B(:,L) swaps the first and fourth columns of B.
(iii) Explain the result of C = A(:)
(iv) Study the function RESHAPE. Use it together with the transpose operator .’ in a single (one-line) command to generate the matrix [123456]
[ 7 8 9 10 11 12]
from A.
Explanation / Answer
part I:
>> I = [1 3]%first row third row which are edge rows
I =
1 3
>> J = [1 4]%first column 4th column which are edge columns
J =
1 4
>> A(I,J)%first row first column, first row last column, third row first column, third row last column
ans =
1 4
9 12
>>
Part II
K = [1 2 3 4];
L = [4 2 3 1];
>> B(:,K) = B(:,L)
B =
4 2 3 1
8 6 7 5
12 10 11 9
here fourth column placed in first column, 2nd col in 2nd col, 3rd in 2rd and 1st in 4th column
Part III
C(:)
it brings all the elements in single column, column by column
>> C = A(:)
C =
1
5
9
2
6
10
3
7
11
4
8
12
>>
part IV:
reshape(A,m,n)
this function reshapes A into m number of rows and n number of column
we want A of 3 by 4 size matrix into 2 by 6 matrix then
>> reshape(A',[],2)'
ans =
1 2 3 4 5 6
7 8 9 10 11 12
>>
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.