Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

[MATLAB] Manipulate a matrix G = [1 2 1 0 0 0 0 3 4 0 1 0 0 0 1 0 1 2 1 3 1 0 1

ID: 3744089 • Letter: #

Question

[MATLAB] Manipulate a matrix

G =
[1 2 1 0 0 0 0
3 4 0 1 0 0 0
1 0 1 2 1 3 1
0 1 2 7 -1 2 1]

Manipulate a matrix: Do the following operations on matrix G created above in Problem 6.
(a) Extract the rst 3X3 submatrix from G and store it in the matrix H, that is, create a matrix
H =
[1 2 1
3 4 0
1 0 1]

by extracting the appropriate rows and columns from the matrix G.


(b) Create the matrix E obtained from H by replacing Hsub22 = 4 by 7. Do not enter E explicitly.
Hint: enter rst E=H; to create copy of the matrix H and then manipulate the matrix E. The
resulting matrix should be
E =
[1 2 1
3 7 0
1 0 1]


(c) Create the matrix F obtained by deleting the third column of the matrix H. Do not enter F
explicitly.
(d) What happens if you type G(:,:) and hit return? Do not include the output in your lab
report, but include a statement describing the output in words.
What happens if you type G(:) and hit return? Do not include the output in your lab report,
but include a statement describing the output in words.
(e) What happens if you type G(5,2) and hit return? Explain.
(f) What happens if you type max(G)? Explain. What happens if you type sum(G)? Explain.
(g) What happens if you type G(G>3) and hit return? Can you explain how MATLAB got that
answer? What happens if you type G(G>3) = 100 and hit return? Can you explain how
MATLAB got that answer?

Explanation / Answer

G =[1 2 1 0 0 0 0
3 4 0 1 0 0 0
1 0 1 2 1 3 1
0 1 2 7 -1 2 1]

Answer a)
=========
H = G(1:3, 1:3)


Answer b)
========
E = H
E(2,2) = 4


Answer c)
=========
F = H(:, 1:2)


Answer D)
=========
G(:,:) will output all rows and all columns of the matrix G. It is same as simply writing G, so it will output the matrix G


------------------------------------------------------------------------------------------------------------------------
G(:) will output all the elements of the matrix G column wise. i.e. first each element of column 1 is output and then column 2 elements and so on. So
G(:) will produce
1
3
1
0
2
4
0
1
1
0
1
2
0
1
2
7
0
0
1
-1
0
0
3
2
0
0
1
1

------------------------------------------------------------------------------------------------------------------------

G(5,2) will cause an error since it is trying to retrieve the element in 5th row and 2nd column, but G has only 4 rows

------------------------------------------------------------------------------------------------------------------------
max(G) will compute maximum element in each column and return the column maximum. So final output will be a row containing all the maximum elements in each column
3 4 2 7 1 3 1

------------------------------------------------------------------------------------------------------------------------
sum(G) will compute the sum for each column . So the output will be single row which contains sum for each column
5 7 4 10 0 5 2
Here 5 is the sum of elements in column 1, 7 is the sum of elements in column 2, and so on
------------------------------------------------------------------------------------------------------------------------
G(G>3) will return the elements in G which are greater than G i.e it outputs 4 and 7. The condition G>3 gets the indices of elements where it is > 3 and then G(G>3) outputs only those elements


Whereas G(G>3) = 100 , will set those elements in G which are greater than 3 to 100 as shown below

G =

1 2 1 0 0 0 0
3 100 0 1 0 0 0
1 0 1 2 1 3 1
0 1 2 100 -1 2 1

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote