How to set a tolerance?

I'm trying to set a tolerance and it's not working. The values for abs(pm0_1-rmp) should return a number smaller than my very large tolerance, but my code still says that the difference between the two is larger than the tolerance. I cannot find what's wrong with my code.
clc
clear all
data_1 = load('DataSet2_Test.txt');
%Characterize columns
t1_1 = data_1(:,1)/1000; % time s
%convert from bits to V
resolution = 2^10-1;
%Data as assigned pairs *10^8 to transfer within the MeV to GeV range
D1_1 = (data_1(:,2)*5)/resolution; D2_1 = (data_1(:,3)*5)/resolution;
D3_1 = (data_1(:,4)*5)/resolution; D4_1 = (data_1(:,5)*5)/resolution;
D5_1 = (data_1(:,6)*5)/resolution; D6_1 = (data_1(:,7)*5)/resolution;
D7_1 = (data_1(:,8)*5)/resolution; D8_1 = (data_1(:,9)*5)/resolution;
%Test 1
%Proton detector
for ap_1 = 1:length(D8_1)
T_1 = [];
compare_1 = D8_1(ap_1);
T_1(1) = compare_1;
T_1(2) = t1_1(ap_1);
index_1 = 2;
for bp_1 = ap_1+1:length(D2_1)
if abs(D2_1(bp_1) - compare_1) < 1e-8
index_1 = index_1 + 1;
T_1(index_1) = t1_1(bp_1);
end
end
full_T_1{ap_1} = T_1;
end
%for 1
pV_repeat_1 = full_T_1{2}(1); %V
pt_ini_1 = full_T_1{2}(2); %t
pt_repeat_1 = full_T_1{2}(3); %t
pdelt_1 = full_T_1{2}(3)-full_T_1{2}(2); %s
%for all
for ap_1 = 1:length(D8_1)
T_1 = full_T_1{ap_1};
if numel(T_1) > 2
pV_rep_1(ap_1) = T_1(1);
pt_in_1(ap_1) = T_1(2);
pt_rep_1(ap_1) = T_1(3);
ptofall_1(ap_1) = pt_rep_1(ap_1) - pt_in_1(ap_1);
else
pV_rep_1(ap_1) = T_1(1);
pt_in_1(ap_1) = T_1(2);
pt_rep_1(ap_1) = NaN;
ptofall_1(ap_1) = NaN;
end
end
ptofall_1;
c = 299792458;
pm0_1 = (D8_1(ap_1)*10^(22))./(ptofall_1.*c*0.10).^2
rmp = 938.272*10^6; %eV/c^2
tolp = 10^30;
if abs(pm0_1-rmp) < tolp;
y = sum(abs(pm0_1-rmp) <= tolp);
disp(y)
fprintf('Protons detected: %3.2f \n', y)
else abs(pm0_1-rmp) > tolp;
fprintf('No Protons detected')
end
abs(pm0_1-rmp)

 Accepted Answer

Stephen23
Stephen23 43 minutes ago
Edited: Stephen23 16 minutes ago
The first rule of debugging: look at the data!
data_1 = readmatrix('DataSet2_Test.txt') % much better than LOAD
data_1 = 120×9
208998 981 940 938 932 956 954 941 927 209082 965 948 938 929 954 943 930 908 209165 949 935 929 923 955 937 917 920 209248 935 927 919 916 942 930 906 911 209332 953 933 920 909 926 919 899 901 209415 959 941 928 916 932 926 895 893 209499 944 927 917 906 922 922 906 914 209582 949 920 910 901 924 927 908 901 209666 942 921 909 900 920 918 904 915 209748 903 899 901 896 917 926 902 903 209832 895 903 897 889 921 914 902 896 209915 906 892 891 889 918 915 904 887 209998 902 899 897 892 925 917 908 902 210082 897 893 889 884 920 921 906 902 210165 890 892 892 890 919 911 901 903
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
%Characterize columns
t1_1 = data_1(:,1)/1000; % time s
%convert from bits to V
resolution = 2^10-1;
%Data as assigned pairs *10^8 to transfer within the MeV to GeV range
D1_1 = (data_1(:,2)*5)/resolution; D2_1 = (data_1(:,3)*5)/resolution;
D3_1 = (data_1(:,4)*5)/resolution; D4_1 = (data_1(:,5)*5)/resolution;
D5_1 = (data_1(:,6)*5)/resolution; D6_1 = (data_1(:,7)*5)/resolution;
D7_1 = (data_1(:,8)*5)/resolution; D8_1 = (data_1(:,9)*5)/resolution;
%Test 1
%Proton detector
for ap_1 = 1:length(D8_1)
T_1 = [];
compare_1 = D8_1(ap_1);
T_1(1) = compare_1;
T_1(2) = t1_1(ap_1);
index_1 = 2;
for bp_1 = ap_1+1:length(D2_1)
if abs(D2_1(bp_1) - compare_1) < 1e-8
index_1 = index_1 + 1;
T_1(index_1) = t1_1(bp_1);
end
end
full_T_1{ap_1} = T_1;
end
%for 1
pV_repeat_1 = full_T_1{2}(1); %V
pt_ini_1 = full_T_1{2}(2); %t
pt_repeat_1 = full_T_1{2}(3); %t
pdelt_1 = full_T_1{2}(3)-full_T_1{2}(2); %s
%for all
for ap_1 = 1:length(D8_1)
T_1 = full_T_1{ap_1};
if numel(T_1) > 2
pV_rep_1(ap_1) = T_1(1);
pt_in_1(ap_1) = T_1(2);
pt_rep_1(ap_1) = T_1(3);
ptofall_1(ap_1) = pt_rep_1(ap_1) - pt_in_1(ap_1);
else
pV_rep_1(ap_1) = T_1(1);
pt_in_1(ap_1) = T_1(2);
pt_rep_1(ap_1) = NaN;
ptofall_1(ap_1) = NaN;
end
end
ptofall_1;
c = 299792458;
pm0_1 = (D8_1(ap_1)*10^(22))./(ptofall_1.*c*0.10).^2
pm0_1 = 1×120
1.0e+09 * 0.7848 0.0017 0.2821 0.0025 0.0104 0.1103 NaN 0.0133 0.0010 6.9519 0.0217 0.0490 0.4397 0.7848 0.0582 0.0104 0.1099 NaN 0.0360 0.0052 0.0035 0.0052 0.0044 0.0023 NaN 0.1443 0.0038 0.0360 0.7848 NaN
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
rmp = 938.272*10^6; %eV/c^2
tolp = 10^30;
Lets take a look at those values that you are trying to compare:
tmp = abs(pm0_1-rmp)
tmp = 1×120
1.0e+09 * 0.1534 0.9365 0.6562 0.9358 0.9278 0.8280 NaN 0.9249 0.9373 6.0136 0.9165 0.8893 0.4986 0.1534 0.8801 0.9278 0.8283 NaN 0.9023 0.9331 0.9348 0.9331 0.9339 0.9359 NaN 0.7940 0.9345 0.9023 0.1534 NaN
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
The seventh value is NaN. Lets see how NaNs compare against your tolerance:
idx = tmp<tolp
idx = 1×120 logical array
Columns 1 through 45 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 0 1 1 1 1 0 1 1 1 1 1 1 1 1 0 1 0 0 0 1 1 Columns 46 through 90 0 1 1 0 0 1 1 1 0 1 1 0 0 1 1 1 1 0 0 1 1 1 1 0 0 1 1 0 0 1 0 1 0 1 1 0 1 1 1 0 1 1 1 0 0 Columns 91 through 120 0 1 0 0 0 0 0 0 0 0 0 1 1 1 0 0 0 1 0 0 0 1 0 0 0 0 0 0 0 0
The IF documentation states that "An expression is true when its result is nonempty and contains only nonzero elements (logical or real numeric). Otherwise, the expression is false." Does your logical data contain only nonzero elements? No, it contains false/zero elements. Therefore both of your IF / ELSEIF comparisons are considered FALSE.
So far it is unclear what the problem is.
I suspect that using you actually should be using indexing in place of an innapropriate IF / ELSEIF, but because you have not described the expected behavior that is just a guess.

More Answers (1)

Steven Lord
Steven Lord 12 minutes ago

0 votes

You indicated you've resolved your problem, but depending on your exact goal some tools that might help you with problems like this in the future:
  • discretize
  • histcounts
  • isapprox
  • ismembertol
  • uniquetol

Asked:

on 22 May 2026 at 13:15

Answered:

on 22 May 2026 at 14:50

Community Treasure Hunt

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

Start Hunting!