# Giving a condition in for loop

1 vue (au cours des 30 derniers jours)
Pallov Anand le 10 Fév 2023
Commenté : Pallov Anand le 10 Fév 2023
Let the initial values p (i) = false; i = 1: 10 and I have a condition say a <= b
What I want is:
1) I will run a loop for i = 1:10.
2) Then I want to know for which i the condition a<= b gets satisfied.
3) Then I want to make all the p's other than the index for condition was satisfied, to be true.
For example: if for i = 2 a<= b gets satisfied then I want to make p(1), p(3), p(4),......p(10) TRUE.
Can anyone give a code for this. Thanks
##### 2 commentairesAfficher AucuneMasquer Aucune
Dyuman Joshi le 10 Fév 2023
How is the condition a<=b related to index i?
Pallov Anand le 10 Fév 2023
Modifié(e) : Pallov Anand le 10 Fév 2023
Actually "a" is distance between two points A and B in X-Y coordinate......Point B is fixed and Point A is changing w.r.t. time. And p encapsulates this i.e. the distance between these two points.
Suppose I have 10 robots. Each robot's current position is (x,y) and fixed point is located at (x_o, y_o). Then p(i) will be sqrt( (x(i) - x_o)^2 + (y(i) - y_o)^2 ).
And b is some constant, lets say 0.5.

Connectez-vous pour commenter.

### Réponse acceptée

Dyuman Joshi le 10 Fév 2023
p=zeros(1,10);
b=0.5;
for idx=1:10
a=hypot(x(idx)-x_o,y(idx)-y>o);
if a<=b
p(setdiff(1:10,idx))=1;
end
end
Another method -
for idx=1:10
a=hypot(x(idx)-x_o,y(idx)-y>o);
if a<=b
p=ones(1,10);
p(idx)=0;
end
end
##### 1 commentaireAfficher -1 commentaires plus anciensMasquer -1 commentaires plus anciens
Pallov Anand le 10 Fév 2023
Thanks a lot

Connectez-vous pour commenter.

### Catégories

En savoir plus sur Loops and Conditional Statements 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