如何在matlab中将整个数据集列从文本数据转换为二进制数据

时间:2014-03-27 20:56:37

标签: matlab

大家好我正在研究一个机器学习项目,但是我很想在matlab中将整个数据集列从文本数据转换为二进制数据。

例如,如果该列包含类似网站类型的描述:

art_entertainment
culture_politics
recreation 
computer_internet
science_technology
health 
religion

因此,在上面的示例中,有七种不同的类型,每种类型都应以这种方式用二进制表示:

art_entertainment: 0000001
culture_politics: 0000010
recreation :0000100
computer_internet: 0001000
science_technology: 0010000
health: 0100000
religion: 1000000

如果有人能帮助我,我需要一个matlab代码

1 个答案:

答案 0 :(得分:0)

假设您在单元格数组上有输入字符串,如下所示:

strings = {
    'art_entertainment'
    'culture_politics'
    'recreation'
    'computer_internet'
    'science_technology'
    'health'
    'religion'
};

然后你可以这样做:

bins = dec2bin(cellfun(@(string) find(strcmp(string, unique(strings, 'stable')), 1, 'first'), strings), 8);

这将是结果:

00000001
00000010
00000011
00000100
00000101
00000110
00000111

如果您有重复的条目,它也会起作用,其中将使用第一个二进制数。例如,如果字符串是:

strings = {'a' 'b' 'c' 'a' 'b'};

结果将是:

00000001
00000010
00000011
00000001
00000010