loop in a loop
9 views (last 30 days)
Show older comments
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
0 Comments
Answers (1)
Andrei Bobrov
on 15 Jun 2017
Edited: Andrei Bobrov
on 15 Jun 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 Comments
Andrei Bobrov
on 16 Jun 2017
Hello Avner! Please accept this answer if it helped solve your problem.
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!