Thread Subject: Write a function called addall in MATLAB that mimics the sum function?

Subject: Write a function called addall in MATLAB that mimics the sum function?

From: Georgy

Date: 21 Mar, 2010 07:45:08

Message: 1 of 5

Hi all! You can't use any predefined functions, but loops, mathematical operations, if statements, and matrices are allowed. This is my code so far:

function Y = addall(X)
total = 0;
[r c] = size(X);
Y = [];
for rows = 1:r
for cols = 1:c

end
end

I can't seem to find out how to mimic the sum function by adding all the columns together first and then the rows. Anyideas?

Subject: Write a function called addall in MATLAB that mimics the sum

From: TideMan

Date: 21 Mar, 2010 09:42:05

Message: 2 of 5

On Mar 21, 8:45 pm, "Georgy " <Georg...@yahoo.com> wrote:
> Hi all! You can't use any predefined functions, but loops, mathematical operations, if statements, and matrices are allowed. This is my code so far:
>
> function Y = addall(X)
> total = 0;
> [r c] = size(X);
> Y = [];
> for rows = 1:r
> for cols = 1:c
>
> end
> end
>
> I can't seem to find out how to mimic the sum function by adding all the columns together first and then the rows. Anyideas?

Another homework cheat.
Figure it out for yourself.

Subject: Write a function called addall in MATLAB that mimics the sum function?

From: Jan Simon

Date: 21 Mar, 2010 19:57:04

Message: 3 of 5

Dear Georgy!

> function Y = addall(X)
> total = 0;
> [r c] = size(X);
> Y = [];
> for rows = 1:r
> for cols = 1:c
>
> end
> end

You start with "Y = []". Usually a sum start with 0. Then one element after the other is added. So you just have to find out how to get the elements from the input matrix.

Good luck, Jan

PS. What does you teacher think about posting the homework in a newsgroup? Teachers are payed for teaching, and you do not ask her or him, but a public newsgroup. Perhaps this get her/him down.

Subject: Write a function called addall in MATLAB that mimics the sum function?

From: Jos (10584)

Date: 21 Mar, 2010 20:34:02

Message: 4 of 5

"Georgy " <Georgy46@yahoo.com> wrote in message <ho4iq4$5vd$1@fred.mathworks.com>...
> Hi all! You can't use any predefined functions, but loops, mathematical operations, if statements, and matrices are allowed. This is my code so far:
>
> function Y = addall(X)
> total = 0;
> [r c] = size(X);
> Y = [];
> for rows = 1:r
> for cols = 1:c
>
> end
> end
>
> I can't seem to find out how to mimic the sum function by adding all the columns together first and then the rows. Anyideas?

SIZE is a predefined function ...

Subject: Write a function called addall in MATLAB that mimics the sum

From: Walter Roberson

Date: 21 Mar, 2010 21:35:59

Message: 5 of 5

Jos (10584) wrote:
> "Georgy " <Georgy46@yahoo.com> wrote in message
> <ho4iq4$5vd$1@fred.mathworks.com>...
>> Hi all! You can't use any predefined functions, but loops,
>> mathematical operations, if statements, and matrices are allowed. This
>> is my code so far:
>>
>> function Y = addall(X)
>> total = 0;
>> [r c] = size(X);
>> Y = [];
>> for rows = 1:r
>> for cols = 1:c
>>
>> end
>> end
>>
>> I can't seem to find out how to mimic the sum function by adding all
>> the columns together first and then the rows. Anyideas?

> SIZE is a predefined function ...

Heh. That's a nasty one to get around. I can get the sum along rows
easily enough without using any predefined functions (other than the
permitted mathematical operations), but without using size() or length()
or transpose(), about all I can think of for the final sum is to use a
try/catch block...

Tags for this Thread

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

rssFeed for this Thread

Contact us at files@mathworks.com