isolating values in a table based on user input

조회 수: 1 (최근 30일)
Andrew Czeizler
Andrew Czeizler 2019년 3월 7일
댓글: Adam Danz 2019년 3월 8일
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
Adam Danz
Adam Danz 2019년 3월 8일
1 correction made. I had to add num2str() in the second sanity check.

댓글을 달려면 로그인하십시오.

추가 답변 (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에서 Dimensionality Reduction and Feature Extraction에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by