# Can someone explain me how to make this loop efficiently?

1 vue (au cours des 30 derniers jours)
Santos García Rosado le 17 Fév 2021
Hello Mathworks community!
I'm having a bit of trouble trying to code a for loop. This is my code so far.
randomNumber1 = 100;
randomNumber2 = 150;
out(1)= 0;
B = 4;
for idx = 2:1:numel(A)
if idx <=(B)
ArrayValue = SomeArray(1);
if % first condition
out(idx) = ((out(idx - 1)) - randomNumber1);
elseif % second condition
out(idx) = ((out(idx - 1)) - randomNumber2);
elseif % third condition
out(idx) = (out(idx - 1) + ArrayValue);
end
elseif idx >(B) && idx <= (2*B)
ArrayValue = SomeArray(2);
if % first condition
out(idx) = ((out(idx - 1)) - randomNumber1);
elseif % second condition
out(idx) = ((out(idx - 1)) - randomNumber2);
elseif % third condition
out(idx) = (out(idx - 1) + ArrayValue);
end
elseif idx >(2*B) && idx <= (3*B)
ArrayValue = SomeArray(3);
% It keeps going until I've used every "SomeArray" value
end
end
As you can see, I have to repeat all the statements as many times as values I have inside my vector: SomeArray
The code works as it should, but I'm well awared that this is not how you do it properlly. I know aswell that it is possible to only state the conditions one time, I'm just not sure how to do it.
Could someone help me out?
Thank's!
Santos
##### 0 commentairesAfficher -2 commentaires plus anciensMasquer -2 commentaires plus anciens

Connectez-vous pour commenter.

### Réponse acceptée

Jon le 17 Fév 2021
I think the key is coming up with an expression that gives you the index for someArray from the current loop index. Something like this:
randomNumber1 = 100;
randomNumber2 = 150;
out(1)= 0;
B = 4;
for idx = 2:1:numel(A)
k = ceil(idx/4); % index into someArray
ArrayValue = SomeArray(k);
if % first condition
out(idx) = ((out(idx - 1)) - randomNumber1);
elseif % second condition
out(idx) = ((out(idx - 1)) - randomNumber2);
elseif % third condition
out(idx) = (out(idx - 1) + ArrayValue);
end
end
##### 2 commentairesAfficher AucuneMasquer Aucune
Jon le 17 Fév 2021
Also you should preallocate your our array to the final size it will reach, you just make it one element long.
So
out = zeros(size(2:1:numel(A)));
Santos García Rosado le 18 Fév 2021
Thank you Jon! Your code was what i was looking for. Indeed, I had preallocated the array but I forgot to cipying it down on the answer. Thank's again.

Connectez-vous pour commenter.

### Plus de réponses (1)

Rik le 17 Fév 2021
Modifié(e) : Rik le 17 Fév 2021
Your intuition is correct; this can indeed be simplified:
for k=1:numel(SomeArray)
if idx <= (k*B)
ArrayValue = SomeArray(k);
break%stop the for loop with k
end
end
##### 1 commentaireAfficher -1 commentaires plus anciensMasquer -1 commentaires plus anciens
Santos García Rosado le 18 Fév 2021
Thank's Rik! Exactly what I was looking for.

Connectez-vous pour commenter.

### Catégories

En savoir plus sur MATLAB dans Help Center et File Exchange

R2020b

### Community Treasure Hunt

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

Start Hunting!

Translated by