In: Computer Science
Consider the Titanic data stored in a structure variable named
T.
Passenger ID Survived
1=Yes, 2=No
Gender
1=Male, 2=Female
Age Fare ($)
T =
121 2 1 22 7.25
243 1 2 38 71.28
432 1 2 26 7.93
564 1 2 35 53.1
856 2 1 35 8.05
As a table of 6 students, complete the following – you may want
to have a couple of students checking the syntax and
output on MATLAB.
1. Draw the table (Titanic data) on the whiteboard.
2. Annotate and provide the syntax to exact the:
a. 1
st column and assign it to the variable named id
b. 3
rd column and assign it to the variable named gender
c. 4
th column and assign it to a variable named age
3. Annotate and provide the output of:
a. index = age>30
b. old_age = age(index)
c. old_age_id = id(index)
4. Write syntax that will return a vector containing all female
passenger IDs
5. Write syntax that will find the row number of the passenger aged
38
6. Write syntax to extract the 2nd and 3rd columns of information
for the passenger aged 38, using the variable created
in step 5
7. Take a photo of your work for future reference. The entire table
is to present the work to a demonstrator and
answer questions to get it marked off before moving onto the next
task.
1)
Passenger ID | Survived | Gender | Age | Fare($) |
---|---|---|---|---|
121 | No | Male | 22 | 7.25 |
243 | Yes | Female | 38 | 71.28 |
432 | Yes | Female | 26 | 7.93 |
564 | Yes | Female | 35 | 53.1 |
856 | No | Male | 35 | 8.05 |
Passenger_ID=[121 243 432 564 856]'
Survived=[2 1 1 1 2]'
Gender=[1 2 2 2 1]'
Age=[22 38 26 35 35]'
Fare=[7.25 71.28 7.93 53.1 8.05]'
tab2=table(Passenger_ID,Survived,Gender,Age,Fare)
2)
a)
id=table2array(tab2(:,1)) % Selecting
1st column and assigning it to Id
id
b)
gender=table2array(tab2(:,3)) %
Selecting 3rd column and assigning it to gender
gender
c)
age=table2array(tab2(:,3)) % Selecting
4th column and assigning it to age
age
3)
a)
index = find(age>30); % Index of
age > 30
index
b)
old_age=age(index) %Ages >30
old_age
c)
old_age_id=id(index)
old_age_id
4)
female=find(gender==2) % we need to
get thhe index details of females
female
female_id=id(female) % selecting female ID's according to index
values
female_id
5)
row_38=find(age==38) % Selecting the
row
row_38
6)
survive=table2array(tab2(:,2)) %select
the second column
sec_col=survive(row_38) %selection of survival age 38
third_col=gender(row_38) %gender of age 38
sec_col,third_col
7)
The MATLAB CODE:-
%Loading the data
Passenger_ID=[121 243 432 564 856]'
Survived=[2 1 1 1 2]'
Gender=[1 2 2 2 1]'
Age=[22 38 26 35 35]'
Fare=[7.25 71.28 7.93 53.1 8.05]'
tab2=table(Passenger_ID,Survived,Gender,Age,Fare)
id=table2array(tab2(:,1))
id
gender=table2array(tab2(:,3)) %3rd column and assign it to the
variable named gender
gender
age=table2array(tab2(:,4)) %4th column and assign it to a
variable named age
age
index = find(age>30);
index
old_age=age(index)
old_age
%old_age id cal
old_age_id=id(index)
old_age_id
%finding the female
female=find(gender==2)
female
%female id
female_id=id(female)
female_id
%index of row having age 38
row_38=find(age==38)
row_38
survive=table2array(tab2(:,2)) %Survive table selection
sec_col=survive(row_38)
third_col=gender(row_38)
sec_col,third_col
Results:-