필터 지우기
필터 지우기

how to use if else statement

조회 수: 4 (최근 30일)
Cside
Cside 2023년 8월 2일
댓글: Cside 2023년 8월 3일
Hello,
I have a list of participants (A, B, C, D...) and would like to extract their data from the dataset X. This dataset has numerous missing data. As there are multiple rows for the same participant, I would like to
  1. to filter all the rows containing specified participant
  2. have an if else statement - if column 4 contains a 'Yes', to insert column 5 'Male' into an array Y. else if column 4 is 'No', to insert column 7 'Female' into the same array Y
  3. ideally, the output should look like a 4x2 array with each unique participant matched to their gender
A Male
B Male
C Female
D Female
I have attached the sample data here.
Thank you very much for your help.
  댓글 수: 2
Dyuman Joshi
Dyuman Joshi 2023년 8월 2일
"how to use if else statement"
I strongly recommend you take the free MATLAB Onramp tutorial to learn the essentials of MATLAB.
Cside
Cside 2023년 8월 3일
thank you!

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

채택된 답변

Clay Swackhamer
Clay Swackhamer 2023년 8월 2일
Hope this helps. It uses an example built in dataset (carbig) to demonstrate some basics of working with data in tables. As with many things there is more than one way to solve this problem. Task 3 (reshaping the output) depends on your specific dataset but I think if you accomplish tasks 1 and 2 you will be able to solve it.
%% Load data
load carbig
% Put the data into a table
% First convert some variables to categorical
org = categorical(cellstr(org));
cyl4 = categorical(cellstr(cyl4));
cars = table(org,Model_Year,when,cyl4,Acceleration,Horsepower,Weight);
%% 1. Filter all rows containing specified participant
% Get all cars from Japan
japaneseCars = cars(cars.org == 'Japan',:)
%% 2. If/else statement
% If the car is a 4 stroke (YES) insert horsepower into an array called "output"
% If the car is is not a 4 stroke (NO) insert weight into the same array
% Go through the table one row at a time
for i = 1:height(japaneseCars)
if japaneseCars.cyl4(i) == 'Four' % YES case
output(i) = japaneseCars.Horsepower(i);
else % NO case
output(i) = japaneseCars.Weight(i);
end
end

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Tables에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by