필터 지우기
필터 지우기

I dont know where i was wrong

조회 수: 1 (최근 30일)
Nguyen Huy
Nguyen Huy 2021년 5월 11일
댓글: Nguyen Huy 2021년 5월 11일
%I am doing project euler 8,this is my code
function y = euler008(x)
x=num2str(x)
d=[]
for i=1:length(x)
number = str2num(x(i:i+4))
digits = num2str(number) - '0'
d=[d prod(digits)]
end
y=max(d)
end
%i dont know why this code is wrong

채택된 답변

Nguyen Huy
Nguyen Huy 2021년 5월 11일
편집: Nguyen Huy 2021년 5월 11일
i have edited all these errors,but it get %Index exceeds matrix dimensions error
  댓글 수: 2
Atsushi Ueno
Atsushi Ueno 2021년 5월 11일
one more thing, indexing shall be within 1 to length(x), so length(x)+4 exceeds the matrix dimensions.
for i=1:length(x)-4
Nguyen Huy
Nguyen Huy 2021년 5월 11일
oh,i forgot that,thanks u all

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

추가 답변 (3개)

Atsushi Ueno
Atsushi Ueno 2021년 5월 11일
There is a variable 'a' in caller workspase and a function euler008(a) is called, but argument in the function euler008(x) is 'x'.
number = str2num(a(i:i+4)
shall be changed to
number = str2num(x(i:i+4))
Also, you need adding missing parenthesis as the first answer.

Atsushi Ueno
Atsushi Ueno 2021년 5월 11일
You are trying to access to variable x's index from 1 (min) to length(x)+4 (max).
for i=1:length(x)
number = str2num(x(i:i+4))
The maximum index number of variable x has been exceeded. If you modify the code to the following, it will work correctly.
for i=1:length(x)-4
number = str2num(x(i:i+4))

Atsushi Ueno
Atsushi Ueno 2021년 5월 11일
>The large number will be given as a string, 1xn characters.
So, x=num2str(x) on 2nd line won't work correctly. It should be deleted.

카테고리

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