Problem 3.23 Write a MATLAB function that will take a (4 x 4) matrix as an input
ID: 441794 • Letter: P
Question
Problem 3.23
Write a MATLAB function that will take a (4 x 4) matrix as an input argument and will return a (4 x 1) array containing the averages of each of the four rows of the input matrix.
Example execution of 3.23:
indata323 =
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
>> lab4_323(indata323)
ans =
2.5000
6.5000
10.5000
14.5000
Hint: Matlab has a built-in sum function to sum the contents of an array row or column. From the command window, enter:
help sum
Use the format:
S = sum(X,DIM) sums along the dimension DIM.
For DIM specify the row and column of the array.
You can use the colon (:) operator for the row or column dimension to sum a row or column. See page 95. For example, to sum column 1 of an array:
column1sum = sum(array(:,1));
Explanation / Answer
clear all; clear screen;
x = input('enter the matrix elements')
rowsum(1,1) = sum(x(1,:))/4;
rowsum(2,1) = sum(x(2,:))/4;
rowsum(3,1) = sum(x(3,:))/4;
rowsum(4,1) = sum(x(4,:))/4;
disp('the averages of the rows are')
disp(rowsum)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.