loop in a loop

13 Ansichten (letzte 30 Tage)
avner yakov
avner yakov am 15 Jun. 2017
Kommentiert: Andrei Bobrov am 16 Jun. 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

Antworten (1)

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

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by