How can I fix a vector value inside for loop when a condition is met ?
3 次查看(过去 30 天)
显示 更早的评论
I tried below to explain what I want to do...Below I want to stop an object from movement when a condition is met, thats why I fixed the angle that the condition is satisfied at,,,,Below I tried to repeat the same angle in each round after the condition satisfying and multiply it by the velocity of object equals zero to make displacement zero and equals the previous position with the new position,, so I stopped the object but after 3~4 rounds then the object starts move which means the angle is changed which is break the condition. I hope if someone have any idea about fixing the angle and dont let it change in the next rounds.
I appreciate any help and thanks in advance
Old_Pos = [ 25 -25]; v1=2;%velocity of an object
vS = 0;
for jj=1:100
for best =1:1 % to choose the best from the inner loop
Ang_1 = [25 65 80 45 60];
options=randperm(numel(Ang_1));
FixedAngle =NaN;
for ii=1:1:numel(options)
angle11= Ang_1(options(ii));
Displacement = [cos(angle11) .* v1 ,sin(angle11) .* v1];
Pos_New = Old_Pos + Displacement;
payoff(ii) = do calculations based on each angle
if Condition
FixedAngle = angle_Max;
Displacement = [cos(FixedAngle) .* vS ,sin(FixedAngle) .* vS]; % make it zero to stop that object
Pos_New = Old_Pos + Displacement;
end
end
angle_Max = one of the angles
Disp_Max = [cos(angle_Max) .* v1 ,sin(angle_Max) .* v1];
Pos_Max = Old_Pos + Disp_Max;
if Condition
Disp_Max = [cos(angle_Max) .* vS ,sin(angle_Max) .* vS]; % to stop the object in the outer loop
Pos_Max = Old_Pos + Disp_Max;
end
end
end
0 个评论
回答(1 个)
Niranjan Sundararajan
2023-7-12
Hey there,
The following code changes will help you fix the angle for the following rounds. You simply check the condition and break from it if the condition is satisfied, thereby not allowing recomputation of the FixedAngle parameter.
FixedAngle = NaN;
FixedAngleSetFlag = false;
for jj = 1:100
for best = 1:1 % to choose the best from the inner loop
Ang_1 = [25 65 80 45 60];
options = randperm(numel(Ang_1));
for ii = 1:numel(options)
angle11 = Ang_1(options(ii));
Displacement = [cos(angle11) .* v1, sin(angle11) .* v1];
Pos_New = Old_Pos + Displacement;
payoff(ii) = do calculations based on each angle
if Condition
FixedAngle = angle11;
FixedAngleSetFlag = true
% Exit after finding fixed angle
break;
end
end
if FixedAngleSetFlag
Displacement = [cos(FixedAngle) .* vS, sin(FixedAngle) .* vS]; % make it zero to stop the object
Pos_New = Old_Pos + Displacement;
end
angle_Max = FixedAngle; % Use the fixed angle as the maximum angle
Disp_Max = [cos(angle_Max) .* v1, sin(angle_Max) .* v1];
Pos_Max = Old_Pos + Disp_Max;
if Condition
Disp_Max = [cos(angle_Max) .* vS, sin(angle_Max) .* vS]; % to stop the object in the outer loop
Pos_Max = Old_Pos + Disp_Max;
end
end
end
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!