필터 지우기
필터 지우기

Interating through a table data set

조회 수: 1 (최근 30일)
Andrew Czeizler
Andrew Czeizler 2019년 3월 5일
답변: Andrew Czeizler 2019년 3월 7일
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