我有一个以制表符分隔的值文件,我加载该文件并将其转换为一个表,其中的值是标量。从表中拾取值时,它不会识别所有值。如果我直接创建与矩阵完全相同的值,它会这样做(tab1.a == tab2.a)'
给出ans = 1 1 0 0 0 0 1
,表明值3到6不同。2.2204e-16
的差异表明这是一个数字精度的问题,但我怎么能允许呢?我使用Octave版本6.4.0
clear all; clc;
pkg load tablicious
function pick = tabpick(tab, a, b);
pick = ((tab.a==a) & (tab.b==b));
if ~sum(pick) % no match
error("Selection not dataset !!n");
endif
pick = table2struct( tab(pick,:));
return
endfunction
% create table direct
m = [ 1.315 1.7
1.315 2.19
1.66 2.1
1.66 2.3
1.66 3.03
1.76 3.28
1.9 2.4 ];
a = m(:,1);
b = m(:,2);
tab1 = table(a, b);
% prettyprint the table
pp(tab1)
% show type of variables
typeinfo(tab1.a), typeinfo(tab1.a(1))
% load table from tsv file
fid = fopen('table_test.txt', 'r');
tab_file = textscan(fid,"%f %f",'delimiter','t', 'Headerlines',1);
a = tab_file{1};
b = tab_file{2};
tab2 = table(a, b);
pp(tab2)
typeinfo(tab2.a), typeinfo(tab2.a(1))
% compare the two: They are different !
(tab1.a == tab2.a)'
% we can pick any value by menu
pick=menu('pick b', cellstr(num2str(tab2.b)));
table2struct( tab2(pick,:))
% pick from tab1 works fine
tabpick(tab1, 1.315, 2.19)
tabpick(tab1, 1.76, 3.28)
% pick from tab2 fails on second one
tabpick(tab2, 1.315, 2.19)
tabpick(tab2, 1.76, 3.28)
差值给出了线索。使用
function pick = tabpick(tab, a, b);
pick = (abs(tab.a-a)< 1e-15) & (abs(tab.b-b) <1e-15);
...