繁体   English   中英

Matlab表-搜索和隔离表中的值

[英]Matlab table - Searching and isolating values in a table

我试图根据用户输入隔离表中的数据。 我有以下-

sex=input("please input the gender (M/F): ", 's');
sysbp= input("enter styloic blood pressure: ");
diabp= input("enter dystolic blood pressure: ");


systoliclow=[-inf;-inf;-inf;-inf;-inf;-inf;-inf;-inf;-inf;-inf;
                120;120;120;120;120;120;120;120;120;120;
                130;130;130;130;130;130;130;130;130;130;
                140;140;140;140;140;140;140;140;140;140;
                160;160;160;160;160;160;160;160;160;160]
systolichigh =[119;119;119;119;119;119;119;119;119;119;
                129;129;129;129;129;129;129;129;129;129;
                139;139;139;139;139;139;139;139;139;139;
                159;159;159;159;159;159;159;159;159;159;
                inf;inf;inf;inf;inf;inf;inf;inf;inf;inf]
diastoliclow=[-inf; -inf ; 80; 80 ; 85; 85 ; 90; 90; 100; 100 ;
                -inf; -inf ; 80; 80 ; 85; 85 ; 90; 90;100;100;
                -inf; -inf ; 80; 80 ; 85; 85 ; 90; 90;100;100 ;
                -inf; -inf ; 80; 80 ; 85; 85 ; 90; 90;100;100 ;
                -inf; -inf ; 80; 80 ; 85; 85 ; 90; 90;100;100  ] 
diastolichigh=[79; 79 ;84; 84; 89; 89; 99; 99; inf; inf;
                79; 79 ;84; 84; 89; 89; 99; 99; inf; inf;
                79; 79 ;84; 84; 89; 89; 99; 99; inf; inf
                79; 79 ;84; 84; 89; 89; 99; 99; inf; inf
                79; 79 ;84; 84; 89; 89; 99; 99; inf; inf]
gender={'F' ;'M'; 'F' ;'M'; 'F' ;'M'; 'F' ;'M';'F' ;'M';
        'F' ;'M'; 'F' ;'M'; 'F' ;'M'; 'F' ;'M';'F' ;'M' ;
        'F' ;'M'; 'F' ;'M'; 'F' ;'M'; 'F' ;'M';'F' ;'M';
        'F' ;'M'; 'F' ;'M'; 'F' ;'M'; 'F' ;'M';'F' ;'M';
        'F' ;'M'; 'F' ;'M'; 'F' ;'M'; 'F' ;'M';'F' ;'M' }
values= [-3 ;0; 0 ;0; 0; 1; 2 ;2; 3 ;3; 
          0;0;0;0;0;1;2;2;3;3; 
          0;1;0;1;0;1;2;2;3;3;
          2;2;2;2;2;2;2;2;3;3;
          3;3;3;3;3;3;3;3;3;3]
bpt= table(systoliclow, systolichigh, diastoliclow,diastolichigh, gender, values)


if sysbp>=bpt.systoliclow && sysbp<=bpt.systolichigh && diabp<=bpt.diastoliclow && diap>=bpt.diastolichigh && ismember(sex{'F','M'});

  test = bpt.values

如何遍历表以根据用户输入获取值。

您可以使用逻辑索引,无需循环:

isIsolated = sysbp>=bpt.systoliclow & sysbp<=bpt.systolichigh & diabp>=bpt.diastoliclow & diabp<=bpt.diastolichigh & strcmp(bpt.gender,sex);
isolatedTable = bpt(isIsolated,:);

对于输入M12085

>> isolatedTable 

isolatedTable =

  1×6 table

    systoliclow    systolichigh    diastoliclow    diastolichigh    gender    values
    ___________    ____________    ____________    _____________    ______    ______

        120            129              85              89           'M'        1   

如果您只对values变量感兴趣:

>> isolatedValues = bpt(isIsolated,end)

isolatedValues =

  2×1 table

    values
    ______

      1   

您可以查询适合用户输入的行,并通过其ID进行迭代:

rows = sysbp>=bpt.systoliclow & sysbp<=bpt.systolichigh & diabp>=bpt.diastoliclow & diabp<=bpt.diastolichigh & strcmp(sex, bpt.gender);

ind = find(rows == 1);

for i=ind
    bpt(i, :)
end

结果:

ans = 

    systoliclow    systolichigh    diastoliclow    diastolichigh    gender    values
    ___________    ____________    ____________    _____________    ______    ______

    -Inf           119             90              99               'F'       2     
    -Inf           119             90              99               'M'       2     

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM