Using if-statements seperatly for each value in an array
9 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Florian Hölscher
le 9 Fév 2022
Commenté : Florian Hölscher
le 9 Fév 2022
I use if-statements which depend on a certain value. Now, instead of this single value, I have an array with a number of values (roughly 500), each representing one machine. Now for each of these values, the if statement should make a decision. I could do this with a loop around the if-statement, running the loop once for each value. However, all of this is iterated for 60.000 time steps or more, so this kind of loop slows down the simulation immensely. Do you see any smarter way of doing what I put in the example code? Basically a way in which in one go, all the if-statement is applied individually to each array value?
numberOfMachines = 500;
x = randi([0,1],numberOfMachines,1);
for n=1:numberOfMachines
if x(n) == 1
y(n) = 10;
else
y(n) = 0;
end
end
0 commentaires
Réponse acceptée
David Hill
le 9 Fév 2022
numberOfMachines = 500;
x = randi([0,1],numberOfMachines,1);
y=zeros(size(x));
y(x==1)=10;
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Genomics and Next Generation Sequencing 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!