Comparing all values from two vectors with statement

1 view (last 30 days)
Hello,
I'm looking for solution to compare two vectors:
traj_cnt = 0;
t_row_cnt = 0;
m_row_cnt = 0;
global_TP = 0;
global_FN = 0;
global_FP = 0;
global_TN = 0;
error_cnt = 0;
motorspeed_detected(length(Traj_interpl)) = 0;
motor_actual(length(M_interpl)-1) = 0;
var = 2;
harm = 10;
TP_help = 0;
FN_help = 0;
FP_help = 0;
TN_help = 0;
TN_debug = 0;
TP=0;
FN=0;
FP=0;
TN=0;
motor_detection_cnt = 0;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Actual time slot calculation
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for t_ref_cnt = 385%1:length(t_ref) %for every 10ms time step
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for m_interpl_cnt = 2:length(M_interpl) %for any motor
for m_row_cnt = 1:length(M_interpl{m_interpl_cnt}) %for any row of single motor
if t_ref(t_ref_cnt) == M_interpl{1}(m_row_cnt) %if right row of motor is choosen
motor_actual(m_interpl_cnt-1) = M_interpl{m_interpl_cnt}(m_row_cnt); %write aktual value of motorspeed to array
break;
else
motor_actual(m_interpl_cnt-1) = 0;
end
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for traj_cnt = 1:length(Traj_interpl) %for every Traj-Cell
for t_row_cnt = 1:length(Traj_interpl{traj_cnt}) %for any row of Traj
if t_ref(t_ref_cnt) == Traj_interpl{traj_cnt}(t_row_cnt,1) %if right row of Traj is choosen
motorspeed_detected(traj_cnt) = Traj_interpl{traj_cnt}(t_row_cnt,2); %write detected value to array
break;
else
motorspeed_detected(traj_cnt) = 0;
end
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Beginn TP/FP
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if (any(motor_actual))
for m_act = 1:length(motor_actual) %for any motor
for s_act = 1:length(motorspeed_detected) %for any detected speed
for harm = 1:10
for tube = var %for variable tube
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% True positive % TP % motor is running, motorspeed detected !!!!!!!!!!HERE IS A PROBLEM
if (any(motorspeed_detected))
if (motorspeed_detected(s_act) >= (motor_actual(m_act) * harm) - tube) && (motorspeed_detected(s_act) <= (motor_actual(m_act) * harm ) + tube) %Right speed detected
global_TP = global_TP + 1; %DEBUG
TP_help = TP_help + 1;
elseif (motorspeed_detected(s_act) > ((motor_actual(m_act) * harm) + tube) && (motorspeed_detected(s_act) < ((motor_actual(m_act) * (harm + 1)))- tube)) %Wrong speed detected -> FN
global_FN = global_FN + 1; %DEBUG
FN_help = FN_help + 1;
else
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% False negative % FN % motor is running, motorspeed not/ wrong detected
elseif (~any(motorspeed_detected))
global_FN = global_FN + 1;
FN_help = FN_help + 1;
else
end
end
end
end
end
elseif (~any(motor_actual))
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% False positive % FP % motor is not running, unknown motorspeed detected
if (any(motorspeed_detected))
global_FP = global_FP + 1;
FP_help = FP_help + 1;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% True negative % TN % motor is not running, no motorspeed detected
elseif (~any(motorspeed_detected))
global_TN = global_TN + 1;
TN_help = TN_help + 1;
TN_debug(TN_help)= t_ref_cnt;
else
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
else
end %end of TP/FP
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if (TP_help >= 1) || (TP_help >= 1 && FN_help >= 1)
TP = TP + 1;
elseif FN_help >= 1 && TP_help == 0
FN = FN + 1;
elseif (FP_help >= 1)
FP = FP + 1;
elseif (TN_help >= 1)
TN = TN + 1;
else
error_cnt = error_cnt + 1;
end
TP_help = 0;
FN_help = 0;
FP_help = 0;
TN_help = 0;
end
Vectors look for actual time slot:
motor_actual =
37.3321 0 0 0
motorspeed_detected =
Columns 1 through 21
0 40.7246 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Columns 22 through 24
0 0 0
So, idea to detect values of all motor_actual values +-tube (tube is just a static value first) and increase TP_help if motorspeed_detected value (37.33+-2)*harm if not to increase FN_help value. (by multiple detection at least one right value - all motorspeed_detected vector passed -> TP_help increase)
I think, I make mistake by using:
if(motorspeed_detected(s_act) >= (motor_actual(m_act) * harm) - tube) && (motorspeed_detected(s_act) <= (motor_actual(m_act) * harm ) + tube)
because it is a logical expression:
(motorspeed_detected(s_act) >= (motor_actual(m_act) * harm) - tube) is while motorspeed_detected(s_act) = 0 and motor_actual(m_act) * harm) = 0
Like a 0 >= 0 - 2 and the second part the same. And -2 && -2 is logical TRUE. So, for all zeros compare is my if-block TRUE. It's not an aim. How can I solve it? Maybe there are also some elegant solution to avoid multiple for-cycles?
Thank you very much in advance!
  1 Comment
Nik Rocky
Nik Rocky on 28 May 2020
I solve it by elimanating a zeros:
% True positive % TP % motor is running, motorspeed detected
if (any(motorspeed_detected)) && (motorspeed_detected(s_act)) && (motor_actual(m_act))
for harm = 1:10
if (motorspeed_detected(s_act) > (motor_actual(m_act) * harm) - var) && (motorspeed_detected(s_act) < (motor_actual(m_act) * harm ) + var)
global_TP = global_TP + 1;
TP_help = TP_help + 1;
else
global_FN = global_FN + 1;
FN_help = FN_help + 1;
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Maybe you have some arguments abour whole code!?

Sign in to comment.

Answers (0)

Community Treasure Hunt

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

Start Hunting!