loop in a loop

10 visualizaciones (últimos 30 días)
avner yakov
avner yakov el 15 de Jun. de 2017
Comentada: Andrei Bobrov el 16 de Jun. de 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

Respuestas (1)

Andrei Bobrov
Andrei Bobrov el 15 de Jun. de 2017
Editada: Andrei Bobrov el 15 de Jun. de 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 comentarios
avner yakov
avner yakov el 16 de Jun. de 2017
thanks allot
Andrei Bobrov
Andrei Bobrov el 16 de Jun. de 2017
Hello Avner! Please accept this answer if it helped solve your problem.

Iniciar sesión para comentar.

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by