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

2) MATLAB has a matrix function called magictn) which creates an nxn square matr

ID: 3588519 • Letter: 2

Question

2) MATLAB has a matrix function called magictn) which creates an nxn square matrix where the sum of every element in every row, every column, and the two diagonals are al equal to each other. Create a 5X5 magic matrix (matrix A) and an 8x8 magic matrix (matrix Z). For each matrix: a) Find the sum of each row and set the sum of row x to the variable row_x b) Find the sum of each column and set the sum of column x to the variable column_x c) Find the sum of each diagonal and set the sum of diagonal left-to right x to the variable diagonal_Ir, and the sum of diagonal right-to left x to the variable diagonal_rl. (Hint - the function fliplr(A) flips a matrix into its mirror image from left to right)

Explanation / Answer

function x = find()
n=5;
m=8;
s=magic(n);
% magic 5
disp('Results for magic matrix :');
disp(s);
disp('sum of elements of each row :');
row_x = sum(s,2);
disp(row_x);
col_x = sum(s,1);
disp('sum of elements of each col :');
disp(col_x);
diagonal_lr = sum(diag(s));
disp('sum of elements of diagonal LR :');
disp(diagonal_lr);
s= fliplr(s);%fliping matrix..
diagonal_rl = sum(diag(s));
disp('sum of elements of diagonal RL :');
disp(diagonal_lr);


%magic 8 matrix

s=magic(m);

disp('Results for magic matrix :');
disp(s);
disp('sum of elements of each row :');
row_x = sum(s,2);
disp(row_x);
col_x = sum(s,1);
disp('sum of elements of each col :');
disp(col_x);
diagonal_lr = sum(diag(s));
disp('sum of elements of diagonal LR :');
disp(diagonal_lr);
s= fliplr(s);%fliping matrix..
diagonal_rl = sum(diag(s));
disp('sum of elements of diagonal RL :');
disp(diagonal_lr);


end

output:

>> find
Results for magic matrix :
17 24 1 8 15
23 5 7 14 16
4 6 13 20 22
10 12 19 21 3
11 18 25 2 9

sum of elements of each row :
65
65
65
65
65

sum of elements of each col :
65 65 65 65 65

sum of elements of diagonal LR :
65

sum of elements of diagonal RL :
65

Results for magic matrix :
64 2 3 61 60 6 7 57
9 55 54 12 13 51 50 16
17 47 46 20 21 43 42 24
40 26 27 37 36 30 31 33
32 34 35 29 28 38 39 25
41 23 22 44 45 19 18 48
49 15 14 52 53 11 10 56
8 58 59 5 4 62 63 1

sum of elements of each row :
260
260
260
260
260
260
260
260

sum of elements of each col :
260 260 260 260 260 260 260 260

sum of elements of diagonal LR :
260

sum of elements of diagonal RL :
260

>>

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