フィルターのクリア

Set unique elements to 0s and 1s matlab

1 回表示 (過去 30 日間)
chiefjia
chiefjia 2021 年 5 月 10 日
回答済み: the cyclist 2021 年 5 月 10 日
Dear MATLAB experts,
I'm working on a table which contains 3 variables, with only 2 unique elements per variable. I'm trying to rename the elements of the variables to 0s and 1s, setting one unique element to 0 and the other to 1. So far I have managed to set each column to dummy variables with the code below, but this is not what I'm looking for, since I just need one column per variable.
categorical_index = 1;
variables_table = table.Properties.VariableNames;
for i=1:width(le)
le.(variables_table{i}) = dummyvar(categorical(table.(categorical_index)));
categorical_index = categorical_index + 1;
end
I would really appreciate your help.
Thank you in advance

採用された回答

the cyclist
the cyclist 2021 年 5 月 10 日
Here is one way. I doubt it is the best.
% Original table with two variables
x = {'a','b','a','a','b'}.';
y = {'c','c','c','d','d'}.';
tbl = table(x,y)
tbl = 5×2 table
x y _____ _____ {'a'} {'c'} {'b'} {'c'} {'a'} {'c'} {'a'} {'d'} {'b'} {'d'}
% Convert to numeric array of unique values
tbl_01 = [findgroups(tbl.x)-1, findgroups(tbl.y)-1]
tbl_01 = 5×2
0 0 1 0 0 0 0 1 1 1

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by