Matlab Fundamental 11 Cont
Matlab Fundamental 11 Cont
Matlab Fundamental 11 Cont
catArray =
categorical(cellArray);
Categorical arrays take up less memory TASK
than cell arrays of strings. Issue the whos x y command to see how much memory each variable
uses.
whos x y
You can see the categories represented in a TASK
categorical array using Save the categories represented in y to cats.
the categories function on the categorical
array. cats = categories(y)
Categorical arrays allow the use of == for TASK
comparison. Create a variable named iC which contains values
of true corresponding to the values in y that equal C.
y == 'A'
ans = iC = y == 'C'
0 0 0 1 0
1 0
TASK INB = Y ~= 'B'
Create a variable named iNB which contains
values of true corresponding to the values
in y that are not equal to B.
11.7 REPRESENTING DISCRETE CATEGORIES: (5/8) PREMIER LEAGUE PLAYERS
TASK players.Position = categorical(players.Position)
Convert the Position variable
in players to a categorical array.
TASK ns = nnz(players.Position == 'striker')
Use the nnz function to count the number of
values that are striker in
the Position variable in players. Save the
result in a variable named ns.
You can view the category names with the TASK
number of elements in each category using Print a summary of the Position variable in players.
the summary function.
summary(players.Team) summary(players.Position)
Arsenal 3
Crystal Palace 1
Everton 1
Liverpool 1
Manchester City 1
Manchester United 1
Tottenham Hotspur 2
v = [ 10 5 0 0 ];
levels = { 'beg' 'mid' 'last' };
categorical(v,[0 5 10],levels)
ans =
last mid beg
beg
If your categories have an inherent ordering TASK
– for example, “low”, “medium”, and “high” – Create an ordinal variable named y with the data from x and levels
you can specify this with an optional represented by xlevels.
property 'Ordinal':
y = categorical(x,1:4,xlevels,'Ordinal',true)
v = [ 10 5 0 0 ];
levels = { 'beg' 'mid' 'last' };
c = categorical(v,...
[0 5 10],levels,...
'Ordinal',true)
c > 'mid'
ans =
1 0 0 0
TASK summary(teamInfo.ManagerNationality)
Print a summary of the categories
in ManagerNationality.
How many managers are from the United TASK
Kingdom (UK)? You can combine the UK Combine the ManagerNationality countries of England, Scotland,
managers into one category, then view and Wales into a category named UK.
the categories to see.
teamInfo.ManagerNationality =
mergecats(teamInfo.ManagerNationality,{'England','Scotlan
d','Wales'},'UK')