n(x)

조회 수: 15 (최근 30일)
Elias Hassan Rezai
Elias Hassan Rezai 2020년 10월 25일
댓글: Elias Hassan Rezai 2020년 10월 25일
can someboduy tell me wqhat is does these three kind of code mean? I mean why code3 is not working and what dows a( r ) actually mean? hwo does these change the layout? by rthe way this is not a homework but only my own thought :)
CODE1)
a=[0 1];
for r=3:100
a(r)=(r-1)+(r-2);
end
disp(a)
CODE2)
a=[0 1];
for r=3:100
a(r)=a(r-1)+a(r-2);
end
disp(a)
CODE3)
a=[0 1];
for r=3:100
a(r)=a(r-1)+a(r-2);
end
disp(a)
  댓글 수: 2
Walter Roberson
Walter Roberson 2020년 10월 25일
? Code 3 is the same as Code 2 and produces the same result.
Elias Hassan Rezai
Elias Hassan Rezai 2020년 10월 25일
I forgot to change that; I meant a=(1) in code 3 :)

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

채택된 답변

drummer
drummer 2020년 10월 25일
편집: drummer 2020년 10월 25일
CODE1)
a=[0 1]; % you tell MATLAB a is a vector
for r=3:100 % the for loop you understand, right?
a(r)=(r-1)+(r-2); % you're attributing to the rth term of your vector a values of (r - 1) + (r - 2)
% the 1st iteration will be a(3) = (3 - 1) + (3 - 2); a(4) = (4 - 1) + (4 - 2); so on...
end
disp(a)
CODE2)
a=[0 1]; %from this line, a(1) = 0 and a(2) = 1.
for r=3:100
a(r)=a(r-1)+a(r-2); % the difference between this and code 1) is that you're performing
% the summation calling the indexes in the vector a, rather than r values.
% so a(3) = a(2) + a(1) = 0 + 1 = 1
% In code 1, a(3) = 2 + 1 = 3
end
disp(a)
Code 3) is the same as 2) Walter mentioned.
Hope that helps...
Cheers
  댓글 수: 1
Elias Hassan Rezai
Elias Hassan Rezai 2020년 10월 25일
thank u so much :)))))

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Language Fundamentals에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by