- #1

- 91

- 0

function totalsum = cellsum(varargin)

%Purpose: Create a function that a accepts an arbitrary number of elements

%and sums the elements of the arguments together.

%Determine number of arguments

n = nargin;

%Set varargin to a vector,x

x = varargin;

%Allocate space

arraysum = zeros(1,n);

%Calculate the sum of each argument using a for loop.

for ii = 1:n

arraysum(ii) = sum(x{ii});

end%for

%Calculate total sum

totalsum = sum(arraysum);

end%function

%Purpose: Write a calling program for function cellsum using given data.

%Define given data.

a = 10;

b = [4;-2;2];

c = [1 0 3; -5 1 2; 1 2 0];

d = [1 5 -2];

%Call program

callsum = cellsum(a,b,c,d);

%print result

fprintf('Total sum is %f.\n' , callsum)

When I run this, I get these errors:

??? In an assignment A(I) = B, the number of elements in B and

I must be the same.

Error in ==> cellsum at 17

arraysum(ii) = sum(x{ii});

Error in ==> hw10num4 at 12

callsum = cellsum(a,b,c,d);

Thanks for the guidance!