Interating through a table data set
1 view (last 30 days)
Show older comments
Andrew Czeizler
on 5 Mar 2019
Answered: Andrew Czeizler
on 7 Mar 2019
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
0 Comments
Accepted Answer
Andrei Bobrov
on 5 Mar 2019
Edited: Andrei Bobrov
on 5 Mar 2019
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)));
0 Comments
More Answers (4)
See Also
Categories
Find more on Orange in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!