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

Do in Matlab Problem #1 1. Create matrix A, which is a 3 × 3 matrix of zeros. 2.

ID: 3754497 • Letter: D

Question

Do in Matlab

Problem #1 1. Create matrix A, which is a 3 × 3 matrix of zeros. 2. Create matrix B, which is a 3 x 4 matrix of zeros. 3. Create matrix C, which is a 3 x 3 matrix of ones. 4. Create matrix D, which is a 5 x 3 matrix of ones. 5. Create matrix E, which is a 4 6 matrix in which all the elements have a value of pi. 6. Use the diag function to create matrix F whose diagonal has values of 1, 2, 3. Extract the diagonal from this matrix and assign it to variable G b. a. Extract the diagonal that runs from lower left to upper right from this matrix and assign it to variable H (Use MATLAB's help document, search for the "fliplr". 7. Create matrix I, J, K which are matrix of zeros the same size as each of the matrices a, b, and c from Problem 1 in the lab work. (Use the "size" function to help you accomplish this task. You need to define matrices a, b and c again in your HW script) 8. Create matrix L, which is a 6 x 6 magic matrix (Use MATLAB's help document, search for "magic" to learn more about this special matrix). a. Find the sum of each of the rows? (use the "sum" function) b. Find the sum of each of the columns? c. Find the sum of the two diagonals? 9. Create matrix M, which is a 3 x 3 matrix extracted from the upper right-hand corner of the magic matrix you created in part 8. ls this also a magic matrix? (write the necessary commands to justify your answer)

Explanation / Answer

clc

% part 1
A = zeros(3, 3)
% part 2
B = zeros(3, 4)
% part 3
C = ones(3, 3)
% part 4
D = ones(5, 3)
% part 5
E = ones(4, 6)*pi
% part 6
F = diag([1 2 3])
% part 6 a
G = diag(F)
% part 6 b
H = diag(fliplr(F))

% Part 7
% assuming the metrices a, b, c are below
% please replace them with the actual ones
a = [1 7 8; 7 3 1];
b = [1 7; 9 1];
c = [2 8 10; 7 1 9; 8 2 5];
[row, col] = size(a);
I = zeros(row, col)
[row, col] = size(b);
J = zeros(row, col)
[row, col] = size(c);
K = zeros(row, col)

% Part 8
L = magic(6)
% part 8 a
rows_sum = sum(L(1:end, :))
% part 8 b
columns_sum = sum(L(:, 1:end))
% part 8 c
diag1_sum = sum(diag(L))
diag2_sum = sum(diag(fliplr(L)))

% part 9
M = L(1:3, 4:6) % extracting upper right 3x3 elements of L
% checking whether its a magic matrix or not
M_rows_sum = sum(M(1:end, :))
M_cols_sum = sum(M(:, 1:end))
M_diag1_sum = sum(diag(M))
M_diag2_sum = sum(diag(M))
fprintf('If all the columns, rows and diagonals sum constitute to the same number in the matrix M %s ', ...
'then M is also a Magic Matrix else not')

COMMAND WINDOW OUTPUT


A =

0 0 0
0 0 0
0 0 0


B =

0 0 0 0
0 0 0 0
0 0 0 0


C =

1 1 1
1 1 1
1 1 1


D =

1 1 1
1 1 1
1 1 1
1 1 1
1 1 1


E =

3.1416 3.1416 3.1416 3.1416 3.1416 3.1416
3.1416 3.1416 3.1416 3.1416 3.1416 3.1416
3.1416 3.1416 3.1416 3.1416 3.1416 3.1416
3.1416 3.1416 3.1416 3.1416 3.1416 3.1416


F =

1 0 0
0 2 0
0 0 3


G =

1
2
3


H =

0
2
0


I =

0 0 0
0 0 0


J =

0 0
0 0


K =

0 0 0
0 0 0
0 0 0


L =

35 1 6 26 19 24
3 32 7 21 23 25
31 9 2 22 27 20
8 28 33 17 10 15
30 5 34 12 14 16
4 36 29 13 18 11


rows_sum =

111 111 111 111 111 111


columns_sum =

111 111 111 111 111 111


diag1_sum =

111


diag2_sum =

111


M =

26 19 24
21 23 25
22 27 20


M_rows_sum =

69 69 69


M_cols_sum =

69 69 69


M_diag1_sum =

69


M_diag2_sum =

69

If all the columns, rows and diagonals sum constitute to the
same number in the matrix M then M is also a Magic Matrix else not
>>

Matlab Code

clc

% part 1
A = zeros(3, 3)
% part 2
B = zeros(3, 4)
% part 3
C = ones(3, 3)
% part 4
D = ones(5, 3)
% part 5
E = ones(4, 6)*pi
% part 6
F = diag([1 2 3])
% part 6 a
G = diag(F)
% part 6 b
H = diag(fliplr(F))

% Part 7
% assuming the metrices a, b, c are below
% please replace them with the actual ones
a = [1 7 8; 7 3 1];
b = [1 7; 9 1];
c = [2 8 10; 7 1 9; 8 2 5];
[row, col] = size(a);
I = zeros(row, col)
[row, col] = size(b);
J = zeros(row, col)
[row, col] = size(c);
K = zeros(row, col)

% Part 8
L = magic(6)
% part 8 a
rows_sum = sum(L(1:end, :))
% part 8 b
columns_sum = sum(L(:, 1:end))
% part 8 c
diag1_sum = sum(diag(L))
diag2_sum = sum(diag(fliplr(L)))

% part 9
M = L(1:3, 4:6) % extracting upper right 3x3 elements of L
% checking whether its a magic matrix or not
M_rows_sum = sum(M(1:end, :))
M_cols_sum = sum(M(:, 1:end))
M_diag1_sum = sum(diag(M))
M_diag2_sum = sum(diag(M))
fprintf('If all the columns, rows and diagonals sum constitute to the same number in the matrix M %s ', ...
'then M is also a Magic Matrix else not')

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