From: Sam on
"Sam " <sam.watts60(a)ntlworld.com> wrote in message <ho7hlj$mq7$1(a)fred.mathworks.com>...
> "us " <us(a)neurol.unizh.ch> wrote in message <ho7h4o$f52$1(a)fred.mathworks.com>...
> > "Sam " <sam.watts60(a)ntlworld.com> wrote in message <ho7gg3$5ht$1(a)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
From: Jan Sieber on
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
From: Oleg Komarov on
"Jan Sieber" <jan.sieber(a)port.ac.uk> wrote in message <ho897d$6gc$1(a)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
From: Oleg Komarov on
"Oleg Komarov" <oleg.komarovRemove.this(a)hotmail.it> wrote in message <ho8brq$nas$1(a)fred.mathworks.com>...
> "Jan Sieber" <jan.sieber(a)port.ac.uk> wrote in message <ho897d$6gc$1(a)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
From: Jan Sieber on
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(a)hotmail.it> wrote in message <ho8cmh$7sj$1(a)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
First  |  Prev  |  Next  |  Last
Pages: 1 2 3 4
Prev: rounding particle value using PSO
Next: Contourf