# Onehotencode function is giving more columns which are filled with zeros than the number of categories.

2 views (last 30 days)
Yagiz Dereboy on 9 Sep 2021
Answered: Yagiz Dereboy on 11 Sep 2021
a = readtable("mushrooms.csv");
aa = categorical(table2array(a));
b = onehotencode(aa(:,1),2);
first column has 2 categories but function creates 23 columns. Which is also the total number of columns in variable a. Also other variables behave the same too and seemingly 2 spesific column in each onehotencoded variable has the true onehotencoded variables. Can you help me please?
##### 0 CommentsShowHide -1 older comments

Sign in to comment.

### Accepted Answer

Mohammad Sami on 10 Sep 2021
Because you are converting the entire matrix in to a categorical matrix, the categories for the entire matrix are common. If you want to have different categories for each column. you will have to convert each columm to caterogical separately.
a = repmat(1:10,10,1);
b = categorical(a)
b = 10×10 categorical array
1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10
categories(b(:,1))
ans = 10×1 cell array
{'1' } {'2' } {'3' } {'4' } {'5' } {'6' } {'7' } {'8' } {'9' } {'10'}
c = array2table(a);
c = varfun(@categorical,c)
c = 10×10 table
categorical_a1 categorical_a2 categorical_a3 categorical_a4 categorical_a5 categorical_a6 categorical_a7 categorical_a8 categorical_a9 categorical_a10 ______________ ______________ ______________ ______________ ______________ ______________ ______________ ______________ ______________ _______________ 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10
categories(c{:,1})
ans = 1×1 cell array
{'1'}
##### 0 CommentsShowHide -1 older comments

Sign in to comment.

### More Answers (1)

Yagiz Dereboy on 11 Sep 2021
Thanks!!
##### 0 CommentsShowHide -1 older comments

Sign in to comment.

### Categories

Find more on Tables in Help Center and File Exchange

R2021a

### Community Treasure Hunt

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

Start Hunting!

Translated by