채택된 답변

Guillaume
Guillaume 2015년 3월 3일

3 개 추천

x = [6 0 8 1 3 0];
validateattributes(x, {'numeric'}, {'integer', 'nonnegative', '<', 10});
y = polyval(x, 10)

댓글 수: 4

Thank you Guillaume, Never knew about the validateattributes. I was thinking of doing it like:
y=str2num(num2str(x)');
Your approach works
>> str2num(sprintf('%1d',x))
ans =
608130
thank you Guillaume , Joseph Cheng and Per isakson
If you're going to go through string conversion, this should be the fastest:
y =str2num(char(x + '0'))
That is add ASCII value of '0' to each number and convert the string back to number.
conversions to / from strings are very slow compared to just multiplications / additions, so my initial solution is probably the most efficient.

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

추가 답변 (1개)

rantunes
rantunes 2015년 3월 3일
편집: Guillaume 2015년 3월 3일

0 개 추천

Hey,
You can also use another (more mathematical) approach:
x = [1;2;3;4;5];
y = 0;
for i = 1:length(x)
y = y + (10^(i-1))*x(length(x)+1-i);
end
Greets

댓글 수: 2

rantunes
rantunes 2015년 3월 3일
sorry for the one line copy-paste ;)
Guillaume
Guillaume 2015년 3월 3일
Fixed the formatting for you.
Your code is what polyval does.

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

카테고리

도움말 센터 및 File Exchange에서 Logical에 대해 자세히 알아보기

태그

질문:

2015년 3월 2일

댓글:

2015년 3월 3일

Community Treasure Hunt

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

Start Hunting!

Translated by