Q:

Assume a matrix variable mat, as in the following example:

0

mat =

 4 2 4 3 2

 1 3 1 0 5

 2 4 4 0 2

The following for loop 

[r, c] = size(mat);

for i = 1:r

 sumprint(mat(i,:))

end

prints this result:

The sum is now 15

The sum is now 25

The sum is now 37

Write the function sumprint.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

sumprint.m

function sumprint(vec)

% Prints the result of a persistent sum

% of the values in vectors that are passed

% Format of call: sumprint(vector)

% Returns the persistent sum of all input vectors

persistent mysum

if isempty(mysum)

 mysum = 0;

end

mysum = mysum + sum(vec);

fprintf('The sum is now %d\n', mysum)

end

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now