Filter löschen
Filter löschen

how to use if else statement

5 Ansichten (letzte 30 Tage)
Cside
Cside am 2 Aug. 2023
Kommentiert: Cside am 3 Aug. 2023
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 Kommentare
Dyuman Joshi
Dyuman Joshi am 2 Aug. 2023
"how to use if else statement"
I strongly recommend you take the free MATLAB Onramp tutorial to learn the essentials of MATLAB.
Cside
Cside am 3 Aug. 2023
thank you!

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Clay Swackhamer
Clay Swackhamer am 2 Aug. 2023
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

Weitere Antworten (0)

Kategorien

Mehr zu Tables finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by