필터 지우기
필터 지우기

How do I create an arithmetic sequence using the counter function, but only using basic mathematics operations (in this case, addition)

조회 수: 2 (최근 30일)
Sq = [2 4 6 8]
N = 3
if (Sq(2) - Sq(1)) == (Sq(end)-Sq(end-1))
d = Sq(2) - Sq(1)
disp("The sequence is arithmetic")
%i would like to use a counter using addition instead of a counter that ranges from values 1 to N. please assist on how to obtain that?
for i = 1:N
Sq(end + 1) = Sq(end)+d;
end
disp(Sq)
end
  댓글 수: 2
John D'Errico
John D'Errico 2023년 3월 15일
Confusing question. Do you want to create a sequence? Or do you want to determine IF a given sequence is arithmetic? The two are very different problems.

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

답변 (1개)

Walter Roberson
Walter Roberson 2023년 3월 15일
편집: Walter Roberson 2023년 3월 15일
i = 1;
while i <= N
sq(end+1) = sq(end) + d;
i = i + 1;
end
  댓글 수: 1
Fangjun Jiang
Fangjun Jiang 2023년 3월 16일
N=10;
d=2;
i = 1;
while i <= N
sq(end+1) = sq(end) + d;
i = i + 1;
end
The end operator must be used within an array index expression.
Would mess up the vector length if "sq" was given an initial value
(1:N)*d

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

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by