for loop indexing and skip numbers

12 vues (au cours des 30 derniers jours)
Damith
Damith le 28 Mai 2014
Commenté : Abraxas le 25 Avr 2019
Hi,
I need to include for loop in MATLAB to start with 2:13. But I need to start with 2,3,4 and skip 5 and again 6,7,8 and skip 9 and 10,11,12 until 13.
How can I implement this is for loop?
Many thanks in advance.

Réponse acceptée

Roger Stafford
Roger Stafford le 28 Mai 2014
You can also use
ix = floor((4*(1:n)+2)/3);
where n is the desired length of the output.
  1 commentaire
Damith
Damith le 28 Mai 2014
Thanks. I appreciate it.

Connectez-vous pour commenter.

Plus de réponses (3)

the cyclist
the cyclist le 28 Mai 2014
for n = [2 3 4 6 7 8 13]
disp(n)
end
  5 commentaires
Damith
Damith le 28 Mai 2014
Thanks. It worked.
Abraxas
Abraxas le 25 Avr 2019
I've been searching 2 days for this answer! Thank you so much!

Connectez-vous pour commenter.


the cyclist
the cyclist le 28 Mai 2014
Yet another method:
idx = bsxfun(@plus,[1 2 3]',1:4:100)
for ii = idx(:)
disp(ii)
end
  1 commentaire
Damith
Damith le 28 Mai 2014
Thanks. it's working too.

Connectez-vous pour commenter.


the cyclist
the cyclist le 29 Mai 2014
Yet another method:
idx = filter([1 1 1 1],[1 1 1 0],1:100)+1

Catégories

En savoir plus sur Startup and Shutdown dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by