필터 지우기
필터 지우기

adding vectors: positive single digit values and returning digits of sum as elements of vector

조회 수: 2 (최근 30일)
I am trying to write a function that takes two vectors of positive single digit integer values as its input and return the digits of the sum as elements of a vector.
I have two vectors but am unsure how to start a function that will allow me to perform what is listed in the previous statement. If someonme could please help with me a starting po0int, it would be appreciated.
  댓글 수: 5
Cedric
Cedric 2013년 10월 30일
편집: Cedric 2013년 10월 30일
Is it the following that you want to do?
x = [1,2,3] ;
y = [7,9] ;
and then, taking these as sets of digits, add the two numbers that they represent the way we do it by hand? I.e. adding 3 and 9, storing 2 and holding 1, etc.. ?

댓글을 달려면 로그인하십시오.

채택된 답변

Image Analyst
Image Analyst 2013년 10월 30일
Please give an example to show what you want. It could either be
theSum = vector1 + vector2;
like Matt J suggested, or
theSum = sum(vector1) + sum(vector2);
which will work for vectors of different lengths. Or maybe you want to interpolate them to make them equal sizes before summing - we have no idea what you really want. Don't use sum as the name of a variable like you did because sum() is the name of a built in function and you'll disable it if you use the same name for your own functions or variables.
  댓글 수: 1
Cedric
Cedric 2013년 10월 30일
My guess is that she wants something like an addition the way we do it by hand, with numbers given by digits stored in vectors.
PS: thank you for your comment yesterday. I replied but quite late!

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Walter Roberson
Walter Roberson 2013년 10월 30일
The question cannot be answered until you reply to the question posed to you when you posted this previously. See http://www.mathworks.co.uk/matlabcentral/answers/91275#comment_176144

Matt J
Matt J 2013년 10월 30일
편집: Matt J 2013년 10월 30일
Hints:
>> x=str2double(char([7 2 4]+'0'))
x =
724
>> y=num2str(x+1)
y =
725
>> whos x y
Name Size Bytes Class Attributes
x 1x1 8 double
y 1x3 6 char

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by