loop in a loop

조회 수: 11 (최근 30일)
avner yakov
avner yakov 2017년 6월 15일
댓글: Andrei Bobrov 2017년 6월 16일
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

답변 (1개)

Andrei Bobrov
Andrei Bobrov 2017년 6월 15일
편집: Andrei Bobrov 2017년 6월 15일
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
avner yakov
avner yakov 2017년 6월 16일
thanks allot
Andrei Bobrov
Andrei Bobrov 2017년 6월 16일
Hello Avner! Please accept this answer if it helped solve your problem.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by