Effacer les filtres
Effacer les filtres

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

2 vues (au cours des 30 derniers jours)
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 commentaires
Fangjun Jiang
Fangjun Jiang le 15 Mar 2023
1:10
ans = 1×10
1 2 3 4 5 6 7 8 9 10
1:2:10
ans = 1×5
1 3 5 7 9
1:3:10
ans = 1×4
1 4 7 10
John D'Errico
John D'Errico le 15 Mar 2023
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.

Connectez-vous pour commenter.

Réponses (1)

Walter Roberson
Walter Roberson le 15 Mar 2023
Modifié(e) : Walter Roberson le 15 Mar 2023
i = 1;
while i <= N
sq(end+1) = sq(end) + d;
i = i + 1;
end
  1 commentaire
Fangjun Jiang
Fangjun Jiang le 16 Mar 2023
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

Connectez-vous pour commenter.

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Produits


Version

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by