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

回答(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