loop in a loop

13 vues (au cours des 30 derniers jours)
avner yakov
avner yakov le 15 Juin 2017
Commenté : Andrei Bobrov le 16 Juin 2017
i'm trying to make loop in a loop so for each index of alpha(the main loop) will path thru the all loop on v_0(the secondary loop) and store the wanted data on new vector. can someone tell me what wrong with my code
v_0=[3:0.1:20];
for i=1:size(alpha)
for j=1:size(v_0)
t=(5/49)*(v_0(j)*sin(a(i))+sqrt(v(j)^2*(sin(a(i)))^2-98/5));
x=v_0(j)*cos(a(i))*t;
v=zeros(size(alpha));
if x>7.15 || x<6.85
v(i)=1;
else
v(i)=0;
end
end
end

Réponses (1)

Andrei Bobrov
Andrei Bobrov le 15 Juin 2017
Modifié(e) : Andrei Bobrov le 15 Juin 2017
v_0=(3:0.1:20);
m = numel(alpha);
n = numel(v_0);
v = zeros(m,n);
for ii=1:m
for jj=1:n
t=(5/49)*(v_0(jj)*sin(a(ii))+sqrt(v_0(jj)^2*(sin(a(ii)))^2-98/5));
x=v_0(jj)*cos(a(ii))*t;
if x>7.15 || x<6.85
v(ii,jj)=1;
end
end
end
without loop for..end
MATLAB R2016b and later
a = a(:);
v_0 = v_0(:)';
t = v_0.*sin(a);
x = 5/49*v_0.*cos(a).*(t+sqrt(t.^2-98/5));
v = x > 7.15 || x < 6.85;
MATLAB R2016a and earlier
a = a(:);
v_0 = v_0(:)';
t = sin(a)*v_0;
x = 5/49*bsxfun(@times,cos(a)*v_0,(t+sqrt(t.^2-98/5)));
v = x > 7.15 || x < 6.85;
  2 commentaires
avner yakov
avner yakov le 16 Juin 2017
thanks allot
Andrei Bobrov
Andrei Bobrov le 16 Juin 2017
Hello Avner! Please accept this answer if it helped solve your problem.

Connectez-vous pour commenter.

Catégories

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

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by