how can i compare each value in a vector?
Info
This question is closed. Reopen it to edit or answer.
Show older comments
hi this is my program, still a beginner. i need to know a way to compare each angle (in the angle vector) with different if statements but its not working! what is wrong??
outcome=[0 0 0 0 0 0 0 0];
angle=[-20 -18.4 -17 -10 10 17 18.4 20];
for i=1:8
if angle(i) <=15.52
outcome(i)=disp('blocked');
elseif angle > 15.52 && angle < 18.1
outcome(i)=disp('goal')
elseif angle >=18.1 && angle <=18.7
outcome(i)=('hit the post')
else
outcome(i)=disp('wide')
end
end
for i=1:8
fprintf('i= %d\t angle= %6.2f \t outcome= %d \n',i,angle(i),outcome(i))
end
Answers (1)
John BG
on 2 Jun 2016
what about the following:
angle=[-20 -18.4 -17 -10 10 17 18.4 20];
outcome=zeros(1,length(angle))
range=[0 15.52 18.1 18.7 360]
% possible angle correction
% for k=1:1:length(angle)
% if angle(k)<0
% angle(k)=angle(k)+360
% end
% end
% another possible angle correction
angle_abs=abs(angle)
for k=1:1:length(angle_abs)
h=histogram(angle_abs(k),range)
nbin=find(h.Values>0)
switch nbin
case 1
outcome(k)=-1
disp('blocked')
case 2
outcome(k)=1
disp('goal');
case 3
outcome(k)=0
disp('hit post')
case 4
outcome(k)=0
disp('out of range')
otherwise
disp('not an angle')
end
end
If you find this answer of any help solving your question,
please click on the thumbs-up vote link or mark is accepted answer,
thanks in advance
John
This question is closed.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!