Interating through a table data set

1 次查看(过去 30 天)
Hi all,
Im trying to learn how to isolate data from control statements in a table.
Observe the below code -
I am getting some strange results.
Any help would be very much appreciated.
Best,
Andrew
age = input("please input the age: ");
sex=input("please input the gender (M/F):", 's');
%create tables%
%age table
agelow=[30;35;40;45;50;55;60;65; 70];
agehigh= [34;39; 44;49;54;59;64;69;74];
pointsfemale =[-9;-4;0;3;6;7;8;8;8];
pointsmale = [-1; 0; 1; 2 ; 3; 4; 5; 6 ; 7];
agetable=table(agelow, agehigh, pointsfemale, pointsmale)
%if age >= agelow & age<=agehigh & sex== 'F'
for row = 1:height(agetable)
if age >=agelow & age <=agehigh & sex =='F'
test=agetable{row,pointsfemale}
else
test=agetable{row, pointsmale}
end
end

采纳的回答

Andrei Bobrov
Andrei Bobrov 2019-3-5
编辑:Andrei Bobrov 2019-3-5
sex = {'F';'M'};
age = [55;40];
[~,~,ii] = histcounts(age,agetable.agelow);
[lo,jj] = ismember(sex,{'F','M'});
at = agetable{:,3:4};
test = at(sub2ind(size(at),ii,jj(lo)));

更多回答(4 个)

Andrew Czeizler
Andrew Czeizler 2019-3-5
Amazing!
But Im not sure whats going on in the code?
Could you possibly help me understand it.
Pretty awesome though!
Thank you :)
Best,
Andrew

Andrew Czeizler
Andrew Czeizler 2019-3-5
pretty brillant!!!
Thank you champion :).
Best,
Andrew

Andrew Czeizler
Andrew Czeizler 2019-3-7
Hi Andrei
I have another table that im struggling with.
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

Andrew Czeizler
Andrew Czeizler 2019-3-7
I was wondering if you could show me some of your magic :).
Many thanks in advance,
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