MATLAB Write a function that will accept any square matrix as an input, the func
ID: 3013612 • Letter: M
Question
MATLAB
Write a function that will accept any square matrix as an input, the function will return a number as described below. Inside the function utilize nested loops (either nested for, nested while, or a combination of for and while) and any required if statements. Divide all elements the original matrix by 2 and take the sum of each column (which will give a row vector). Then multiply all elements the original matrix by 2 and take the sum of each row (which will give a column vector). Multiply the two vectors together and return the resulting number. You may need to use more than one set of nested loops. You can only use the built in functions size and length. Test your function on the matrix below. Your answer should be 729.
1 2 3
4 5 6
7 8 9
Explanation / Answer
The following code gives the answer 729
%cheggmatrixinput
clear all
N = input('Enter the dimension of square matrix')
column = 1;
row = 1;
for i = 1:N^2
A(row,column) = input('enter elements rowwize');
column = column+1;
if column == N+1
row = row +1;
column= 1;
end
end
%Dividing all elements by 2
for i = 1:N
j = 1:N
A(i,j) = A(i,j)/2;
end
%summing each column
for j = 1:N
colsum(j) = 0;
for i = 1:N
colsum(j) = colsum(j) + A(i,j);
end
end
%multiplying 2 for all elements
%since now A = A/2 and we have to do 2*(Original A)
% we do 4*A now
for i = 1:N
j = 1:N
A(i,j) = A(i,j)*4;
end
%finding row sum
for i = 1:N
rowsum(i,1) = 0;
for j = 1:N
rowsum(i,1) = rowsum(i) + A(i,j);
end
end
answer = colsum*rowsum
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.