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

1. Given a matrix variable \"mat\", write code using for loops, if statements, e

ID: 3798728 • Letter: 1

Question

1. Given a matrix variable "mat", write code using for loops, if statements, etc. that will accomplish the same as the following:

Please make sure to store the result in "matsum" and dont use sum.

This is what I have so far but it keeps telling me is wrong....

% mat has been initialized for you:

mat = randi([-100,100],randi([15,20]),randi([15,20]));

% write your statements that accomplish the code above:

[r c]=size(mat);

for i=1:c

matsum=0;

for j=1:r

matsum=matsum+mat(j,i);

end

fprintf('Sum of column %d:',i)

disp(matsum);

end

Explanation / Answer

Please find the matlab code below for sum of transpose of a matrix.

% matrix initialization
mat = randi([-100,100],randi([15,20]),randi([15,20]));

% Expected output: disp(sum(mat'));
% Rows = 20, Columns = 18
[r c] = size(mat);
% Traversing through rows.
for i = 1:r
matsum = 0;
% Traversing through columns.
for j = 1:c
% Add the entries of each row.
matsum = matsum + mat(i, j);
end
fprintf('Sum of column %d:', i)
disp(matsum);
end

% matrix initialization
mat = randi([-100,100],randi([15,20]),randi([15,20]));

% Expected output: disp(sum(mat'));
% Rows = 20, Columns = 18
[r c] = size(mat);
% Traversing through rows.
for i = 1:r
matsum = 0;
% Traversing through columns.
for j = 1:c
% Add the entries of each row.
matsum = matsum + mat(i, j);
end
fprintf('Sum of column %d:', i)
disp(matsum);
end