i have a data base with name gender and age not necessarily in the same order, some data base is gender name age. for example
'name' 'gender' 'age'
'mary' 'f' 25
'john' 'm' 35
'anna' 'f' 30
'paul' 'm' 22
'elaina' 'f' 38
if i have to use this database as cell array and only get the names of male how i do so?

 채택된 답변

Dave B
Dave B 2021년 11월 17일
편집: Dave B 2021년 11월 17일

0 개 추천

Your data look similar to this (as you've described them):
load patients
data=[{'name'} {'gender'} {'age'}; LastName Gender num2cell(Age)]
You just use strcmp to compare strings, and specify you want the rows where gender contains 'm' and column 1
data(strcmp(data(:,2),'Male'),1) % obviously you'll use 'm' in your case
ans = 47×1 cell array
{'Smith' } {'Johnson' } {'Wilson' } {'Moore' } {'Jackson' } {'White' } {'Martin' } {'Thompson' } {'Martinez' } {'Robinson' } {'Hall' } {'Hernandez'} {'King' } {'Scott' } {'Green' } {'Baker' } {'Nelson' } {'Mitchell' } {'Perez' } {'Roberts' } {'Turner' } {'Phillips' } {'Parker' } {'Edwards' } {'Collins' } {'Stewart' } {'Reed' } {'Bell' } {'Murphy' } {'Ward' }
If it were me, I'd put these in a table and use strings, it's so much better!
t=array2table(data(2:end,:),'VariableNames',data(1,:));
t.name=string(t.name);
t.gender=categorical(t.gender);
t.age=cell2mat(t.age);
t.name(t.gender=="Male")
ans = 47×1 string array
"Smith" "Johnson" "Wilson" "Moore" "Jackson" "White" "Martin" "Thompson" "Martinez" "Robinson" "Hall" "Hernandez" "King" "Scott" "Green" "Baker" "Nelson" "Mitchell" "Perez" "Roberts" "Turner" "Phillips" "Parker" "Edwards" "Collins" "Stewart" "Reed" "Bell" "Murphy" "Ward"

댓글 수: 5

Manav Divekar
Manav Divekar 2021년 11월 17일
I can use first option but if the postion of gender is changed in certain database. like
'gender''name' 'age'
'f' 'mary' 25
'm' 'john' 35
'f' 'anna' 30
'm' 'paul' 22
'f' 'elaina' 38
using data(:,2) can not be used. how can i generlize that?
if i have to use this for a certain age like between 30 and 40
this what i have done so far
function [patient] = filterpatients_cell(data)
patient = data(strcmp(data(:,2),'f'),1) && data(strcmp(data(:,3),'age'=< 30 && 'age' >=40),1);
Dave B
Dave B 2021년 11월 17일
편집: Dave B 2021년 11월 18일
Re how to generalize, this is why using a table makes more sense, because then you can refer to a variable name instead of a number. However you could look at the first row with the same principal
load patients
data=[{'name'} {'gender'} {'age'}; LastName Gender num2cell(Age)];
col = strcmp(data(1,:),'gender');
data(strcmp(data(:,col),'Male'),1)
ans = 47×1 cell array
{'Smith' } {'Johnson' } {'Wilson' } {'Moore' } {'Jackson' } {'White' } {'Martin' } {'Thompson' } {'Martinez' } {'Robinson' } {'Hall' } {'Hernandez'} {'King' } {'Scott' } {'Green' } {'Baker' } {'Nelson' } {'Mitchell' } {'Perez' } {'Roberts' } {'Turner' } {'Phillips' } {'Parker' } {'Edwards' } {'Collins' } {'Stewart' } {'Reed' } {'Bell' } {'Murphy' } {'Ward' }
Re how to apply to age
  • this approach won't work, 'age' can't be compared to 30.
  • strcmp just compares strings, it's not useful for numbers.
  • =< is not a matlab operation
  • If age a cell, you'll have to convert it to a matrix, and then it'll be off by one row because of the headings.
  • Really just use a table, your life will be much easier!
Manav Divekar
Manav Divekar 2021년 11월 18일
with this code im getting an emply cell.
Dave B
Dave B 2021년 11월 18일
Sorry i've updated the code, and added a demo (but really my intent ws to give you a strategy you can use rather than to write the code for you).

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Cell Arrays에 대해 자세히 알아보기

제품

릴리스

R2021b

태그

질문:

2021년 11월 17일

댓글:

2021년 11월 18일

Community Treasure Hunt

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

Start Hunting!

Translated by