isolating values in a table based on user input

1 次查看(过去 30 天)
Hi All,
Hope all is well. I am trying to isolate data in a table based on user input. I have the below-
How do I loop through the table to get the values based on the user input. Many thanks, Best, Andrew
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
Many thanks in advance.
Best,
Andrew
  2 个评论
Adam Danz
Adam Danz 2019-3-7
Let's say my inputs were "M", "117" and "71". What would you like to do with those data?
Andrew Czeizler
Andrew Czeizler 2019-3-7
117 is between -inf and 119
71 is between -inf and 79
sex is male
so value is 0
i want to be able to find value based on input.
many thanks,
best,
Andrew

请先登录,再进行评论。

采纳的回答

Adam Danz
Adam Danz 2019-3-7
编辑:Adam Danz 2019-3-8
This solution creates a set of logical vectors that identify rows of dpt table that are satisfied by the user input. It then combines those logical vectors to identify the row(s) that is satisfied by all inputs. Next I added a sanity check that makes sure that one and only one row was selected. Then it returns the 'value' of that row.
isGenderMatch = strcmpi(bpt.gender, sex); % index of rows that match sex
isLessThanSys = sysbp < bpt.systolichigh; % index of rows where systl bp is less than 'high'
isGreaterThansSys = sysbp >= bpt.systoliclow; % index of rows where systl bp is greater or == than 'low'
isLessThanDia = diabp < bpt.diastolichigh; % index of rows where diast bp is less than 'high'
isGreaterThansDia = diabp >= bpt.diastoliclow; % index of rows where diast bp is greater or == than 'low'
rowIdx = isGenderMatch & isLessThanSys & isGreaterThansSys & isLessThanDia & isGreaterThansDia; %row index of all match(es)
% Sanity check
if sum(rowIdx) == 0
error('No matches found.')
end
if sum(rowIdx) > 1
error('More than 1 match found! Rows [%s].', num2str(find(rowIdx)))
end
% Return value of matching row
test = bpt.values(rowIdx);

更多回答(1 个)

Andrew Czeizler
Andrew Czeizler 2019-3-7
Thank you so much for your help!!
Really clear, what a champion!
Best,
Andrew

类别

Help CenterFile Exchange 中查找有关 Analysis of Variance and Covariance 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by