Assign a categorical value to a table

3 views (last 30 days)
Sarah DS
Sarah DS on 4 May 2022
Commented: Sarah DS on 4 May 2022
Hello, Matlab community. I have a table that has 60 rows and 4 columns. The table is a feature extraction table to be fed into a classifier. My question is, how do I assign categorical values to the first 30 rows of the table? Let's say "Running", from the 31 to 41 "Walking", from the row 41 to 51 "Upstairs" and "downstairs from the row 51 to 60. At the moment, I assume I can't put this table into the KNN classifier because It does not have any separate categories. It is just a massive table with no categories.
I have tried using the categorical function; however, it just assigns a name to the categories, not the range from where they go in the feature table.

Accepted Answer

dpb on 4 May 2022
N=[30,10,10,10]; % the number of the four categories each
C=["Running";"Walking","Upstairs","Downstairs"]; % the categories
tmp=arrayfun(@(c,n)repmat(c,n,1),C,N,'UniformOutput',false); % build the sets
tYourTable.Categories=categorical(vertcat(tmp{:})); % and add the categories variable
One would presume there's some other variable inside the table from which the condition could be deduced, however, in which case one could use that logical condition from which to build the categories without the need to manually assign the counts.

More Answers (0)




Community Treasure Hunt

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

Start Hunting!