从文件加载时 Octave 无法识别值

发布于 2025-01-14 17:41:06 字数 1626 浏览 1 评论 0原文

我有一个制表符分隔值文件,我加载该文件并将其转换为 tablicious 表,其中值是标量。 从表中选取值时,它无法识别所有值。如果我直接创建与矩阵完全相同的值,它就会这样做。
(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)
    

I have a tab separated values file which I load and convert to a tablicious table where the values are scalars.
When picking values from the table it does not recognize all values. If I create the exact same values direct as a matrix it does.
(tab1.a == tab2.a)' gives ans = 1 1 0 0 0 0 1 showing that values 3 to 6 are different. Difference of 2.2204e-16 indicates it's a numeric accuracy thing, but how can I allow for that?
I'm on Octave version 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)
    

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夜夜流光相皎洁 2025-01-21 17:41:06

差值给出了线索​​。与...一起工作

    function pick = tabpick(tab, a, b);
      pick = (abs(tab.a-a)< 1e-15) & (abs(tab.b-b) <1e-15);
      ...

The difference value gave the clue. Working with

    function pick = tabpick(tab, a, b);
      pick = (abs(tab.a-a)< 1e-15) & (abs(tab.b-b) <1e-15);
      ...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文