Thread Subject: adding vectors of different length

Subject: adding vectors of different length

From: Sam

Date: 22 Mar, 2010 09:43:03

Message: 1 of 16

Hi all,

I need to write a code for adding two vectors of different length, e.g. x=[1 9 8] and y=[2 0 4 9], treating the row vectors as integers. Any help will be appreciated.

Subject: adding vectors of different length

From: Jan Simon

Date: 22 Mar, 2010 09:50:04

Message: 2 of 16

Dear Sam!

> I need to write a code for adding two vectors of different length, e.g. x=[1 9 8] and y=[2 0 4 9], treating the row vectors as integers. Any help will be appreciated.

But what do you want or expect as result???
What does "add" mean, if the vectors have different sizes?!

Kind regards, Jan

Subject: adding vectors of different length

From: us

Date: 22 Mar, 2010 09:57:03

Message: 3 of 16

"Sam " <sam.watts60@ntlworld.com> wrote in message <ho7e37$mnn$1@fred.mathworks.com>...
> Hi all,
>
> I need to write a code for adding two vectors of different length, e.g. x=[1 9 8] and y=[2 0 4 9], treating the row vectors as integers. Any help will be appreciated.

show CSSM an small example of an input and an expected output...

us

Subject: adding vectors of different length

From: Walter Roberson

Date: 22 Mar, 2010 09:58:39

Message: 4 of 16

Sam wrote:

> I need to write a code for adding two vectors of different length, e.g.
> x=[1 9 8] and y=[2 0 4 9], treating the row vectors as integers. Any
> help will be appreciated.

What kind of result are you expecting? If you are expecting every
combination of additions, 1+2, 9+4, 8+0 and so on, then see bsxfun(). If
you are expecting element-wise addition, then is the implication that
the shorter vector is to be extended with zeros before doing the additions?

Subject: adding vectors of different length

From: Sam

Date: 22 Mar, 2010 09:59:04

Message: 5 of 16

"Jan Simon" <matlab.THIS_YEAR@nMINUSsimon.de> wrote in message <ho7egc$sh0$1@fred.mathworks.com>...
> Dear Sam!
>
> > I need to write a code for adding two vectors of different length, e.g. x=[1 9 8] and y=[2 0 4 9], treating the row vectors as integers. Any help will be appreciated.
>
> But what do you want or expect as result???
> What does "add" mean, if the vectors have different sizes?!
>
> Kind regards, Jan

Thanks for answering Jan.

I need to add zeros at the front of the smaller number so that I can add the two up. For example x would become [0 1 9 8]. Also when they add up I need the number to extend as they must all be single digits. [9] + [8] must become [1 7] not [17].

Sam

Subject: adding vectors of different length

From: Husam Aldahiyat

Date: 22 Mar, 2010 10:00:06

Message: 6 of 16

"Sam " <sam.watts60@ntlworld.com> wrote in message <ho7e37$mnn$1@fred.mathworks.com>...
> Hi all,
>
> I need to write a code for adding two vectors of different length, e.g. x=[1 9 8] and y=[2 0 4 9], treating the row vectors as integers. Any help will be appreciated.

>> bsxfun(@plus,x,y')

Subject: adding vectors of different length

From: Sam

Date: 22 Mar, 2010 10:24:03

Message: 7 of 16

I have a first line for my function

function res=MyAdd (x,y)

not really sure where to go after this

Subject: adding vectors of different length

From: Husam Aldahiyat

Date: 22 Mar, 2010 10:33:04

Message: 8 of 16

function res = MyAdd (x,y)

a = [zeros(1,length(x)-length(y)),y];
b = [zeros(1,length(y)-length(x)),x];

res = str2num(reshape([(num2str(a+b));...
repmat(' ',1,length((num2str(a+b))))],1,[]));

end

Subject: adding vectors of different length

From: us

Date: 22 Mar, 2010 10:35:04

Message: 9 of 16

"Sam " <sam.watts60@ntlworld.com> wrote in message <ho7gg3$5ht$1@fred.mathworks.com>...
> I have a first line for my function
>
> function res=MyAdd (x,y)
>
> not really sure where to go after this

that is not a lot...
did you come up with an example as you were told(?)...

us

Subject: adding vectors of different length

From: Sam

Date: 22 Mar, 2010 10:44:04

Message: 10 of 16

"us " <us@neurol.unizh.ch> wrote in message <ho7h4o$f52$1@fred.mathworks.com>...
> "Sam " <sam.watts60@ntlworld.com> wrote in message <ho7gg3$5ht$1@fred.mathworks.com>...
> > I have a first line for my function
> >
> > function res=MyAdd (x,y)
> >
> > not really sure where to go after this
>
> that is not a lot...
> did you come up with an example as you were told(?)...
>
> us

yes i do have an example x=[1 9 8], y=[2 0 4 9]

i have to add them up as if it were [0 1 9 8] and [2 0 4 9]

Subject: adding vectors of different length

From: Sam

Date: 22 Mar, 2010 11:22:02

Message: 11 of 16

"Sam " <sam.watts60@ntlworld.com> wrote in message <ho7hlj$mq7$1@fred.mathworks.com>...
> "us " <us@neurol.unizh.ch> wrote in message <ho7h4o$f52$1@fred.mathworks.com>...
> > "Sam " <sam.watts60@ntlworld.com> wrote in message <ho7gg3$5ht$1@fred.mathworks.com>...
> > > I have a first line for my function
> > >
> > > function res=MyAdd (x,y)
> > >
> > > not really sure where to go after this
> >
> > that is not a lot...
> > did you come up with an example as you were told(?)...
> >
> > us
>
> yes i do have an example x=[1 9 8], y=[2 0 4 9]
>
> i have to add them up as if it were [0 1 9 8] and [2 0 4 9]

my function has to look like this in the command line

>> s=MyAdd([1 8 9],[2 0 4 9])
s =
2 2 3 8
>> MyAdd(9*ones(1,40),7*ones(1,39))
ans =
Columns 1 through 13
1 0 7 7 7 7 7 7 7 7 7 7 7
Columns 14 through 26
7 7 7 7 7 7 7 7 7 7 7 7 7
Columns 27 through 39
7 7 7 7 7 7 7 7 7 7 7 7 7
Columns 40 through 41
7 6

Subject: adding vectors of different length

From: Jan Sieber

Date: 22 Mar, 2010 17:26:05

Message: 12 of 16

This was posed as a (very) basic programming exercise (NUM101) to let the students translate an algorithm they know without guidance into a program. MyAdd is supposed to treat the vectors full of single digits as arbitrary-length integers.
That's why

>> z=MyAdd([1 9 8],[2 0 4 9])
z =
     2 2 4 7

Subject: adding vectors of different length

From: Oleg Komarov

Date: 22 Mar, 2010 18:11:06

Message: 13 of 16

"Jan Sieber" <jan.sieber@port.ac.uk> wrote in message <ho897d$6gc$1@fred.mathworks.com>...
> This was posed as a (very) basic programming exercise (NUM101) to let the students translate an algorithm they know without guidance into a program. MyAdd is supposed to treat the vectors full of single digits as arbitrary-length integers.
> That's why
>
> >> z=MyAdd([1 9 8],[2 0 4 9])
> z =
> 2 2 4 7



function Out = myAdd(A,B)
    % Transform vector A and B to numbers and take the sum
    numAdd = (sum(10.^(numel(A):-1:1).*A) + sum(10.^(numel(B):-1:1).*B))/10;
    % Convert into string and separate each char in different cells converting to double
    Out = arrayfun(@(x) {str2double(x)},num2str(numAdd));
    % Take out from cell
    Out = cat(2,Out{:});
end

Oleg

Subject: adding vectors of different length

From: Oleg Komarov

Date: 22 Mar, 2010 18:25:21

Message: 14 of 16

"Oleg Komarov" <oleg.komarovRemove.this@hotmail.it> wrote in message <ho8brq$nas$1@fred.mathworks.com>...
> "Jan Sieber" <jan.sieber@port.ac.uk> wrote in message <ho897d$6gc$1@fred.mathworks.com>...
> > This was posed as a (very) basic programming exercise (NUM101) to let the students translate an algorithm they know without guidance into a program. MyAdd is supposed to treat the vectors full of single digits as arbitrary-length integers.
> > That's why
> >
> > >> z=MyAdd([1 9 8],[2 0 4 9])
> > z =
> > 2 2 4 7
>
>
>
> function Out = myAdd(A,B)
> % Transform vector A and B to numbers and take the sum
> numAdd = (sum(10.^(numel(A):-1:1).*A) + sum(10.^(numel(B):-1:1).*B))/10;
> % Convert into string and separate each char in different cells converting to double
> Out = arrayfun(@(x) {str2double(x)},num2str(numAdd));
> % Take out from cell
> Out = cat(2,Out{:});
> end
>
> Oleg

An alternative would be to use, right after numAdd:
    % Number of digits
    numDgt = floor(log10(numAdd)+1):-1:1;
    % Use modulus and rounding
    Out = floor(mod(numAdd,10.^numDgt)./10.^(numDgt-1));

Oleg

Subject: adding vectors of different length

From: Jan Sieber

Date: 22 Mar, 2010 19:28:05

Message: 15 of 16

This defeats the purpose of being able to do arbitrarily large integers: what about
MyAdd(ones(1,40000)*9,ones(1,39999)*7)?
(That is 999...999+77...777.)

The idea was that one cannot do it without loop, and that the students have to construct the loop themselves. So, your solution would not help. I'd suggest to let the students come up with the solutions themselves, and not doing their homework for them. It really does not help them in the long run: the problem here is not Matlab but the basic ability to express a known algorithm in a computer language.

Jan

"Oleg Komarov" <oleg.komarovRemove.this@hotmail.it> wrote in message <ho8cmh$7sj$1@fred.mathworks.com>...
> > % Transform vector A and B to numbers and take the sum
> > numAdd = (sum(10.^(numel(A):-1:1).*A) + sum(10.^(numel(B):-1:1).*B))/10;
> > % Convert into string and separate each char in different cells converting to double
> > Out = arrayfun(@(x) {str2double(x)},num2str(numAdd));
> > % Take out from cell
> > Out = cat(2,Out{:});
> > end

Subject: adding vectors of different length

From: Oleg Komarov

Date: 22 Mar, 2010 20:30:40

Message: 16 of 16

"Jan Sieber" <jan.sieber@port.ac.uk> wrote in message <ho8gc5$bsd$1@fred.mathworks.com>...
> This defeats the purpose of being able to do arbitrarily large integers: what about
> MyAdd(ones(1,40000)*9,ones(1,39999)*7)?
> (That is 999...999+77...777.)

That's right.

> The idea was that one cannot do it without loop, and that the students have to construct the loop themselves. So, your solution would not help. I'd suggest to let the students come up with the solutions themselves, and not doing their homework for them. It really does not help them in the long run: the problem here is not Matlab but the basic ability to express a known algorithm in a computer language.


Totaly agree about the student thing, sorry if I didn't catch the message in your first post.
Not totally sure about "The idea was that one cannot do it without loop"

Regards

Oleg

Tags for this Thread

Everyone's Tags:

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.

Tag Activity for This Thread
Tag Applied By Date/Time
homework Jan Sieber 22 Mar, 2010 15:29:10
programming basics Jan Sieber 22 Mar, 2010 13:29:07
rssFeed for this Thread

Contact us at files@mathworks.com