In this exercise, you will create, if possible, an m by n matrix A, such that, a
ID: 1715132 • Letter: I
Question
In this exercise, you will create, if possible, an m by n matrix A, such that, an (i, j)-entry of the matrix A is the sum of the indexes i and j, where i runs from 1 to m and j runs from 1 to n. You can use for loop for constructing matrix A.
You will also check in your code whether the given variables m and n are integer elements. The conditional statement “if … else”
and the MATLAB function mod can be used here. If both m and n are integer, proceed to the calculation of the entries of the matrix A. If not, the outputs have to be “A = empty_matrix” followed by the comment “m and n must be integer”.
Please follow the steps outlined below:
**Create the function in MATLAB
A=sums(m,n)
Explanation / Answer
Create a function in the script file for [A]=sums(m,n).
function[A]=sums(m,n)
if (m>0 && n>0 && mod(m,1)==0 && mod(n,1)==0)
disp('m and n are integers')
for i=1:m
for j=1:n
A(i,j)=i+j;
end
end
else
A=[ ] % [ ] is a empty matrix
disp('m and n must be integers')
end
end
Test 1: First test for non-integers.
A=sums(2.3,4)
m and n must be integers
A =
[ ]
Test 2: Test for negative row/column size.
A=sums(-2,4)
m and n must be integers
A =
[ ]
Test 3: Test for with correct values.
A=sums(2,4)
m and n are integers
A =
2 3 4 5
3 4 5 6
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.