필터 지우기
필터 지우기

how can I write a function

조회 수: 4 (최근 30일)
Manav Divekar
Manav Divekar 2021년 11월 17일
댓글: Dave B 2021년 11월 18일
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일
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월 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개)

카테고리

Help CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

태그

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by