0

我已经在 Matlab 表的上下文中将我的脚趾浸入了 Matlab 的分类变量池。其实,我过去可能也曾在那个领域徘徊过,但如果是这样,那也算是比较肤浅的了。

这些天来,我想使用 Matlab 代码模式来做我通常在 MS Access 中会做的事情,例如各种类型的连接和过滤。我的大部分数据都是分类数据,并且我已经阅读了在表格中使用分类变量的优势。但是,它们主要围绕描述性(超过枚举类型)和内存效率。我没有提到速度。分类变量是否提供速度优势?

我还想知道在进行各种类型的连接时使用分类变量是多么可取。分类变量将占用不同的表,因此我不清楚如果 SQLON子句中涉及此类变量(Matlab 将其称为keys参数),如何建立值的等价性。

由于缺乏相关的谷歌点击,我似乎进入了一个新的领域,这对我来说是一件可怕的事情。缺乏最佳实践的文档,以及由此产生的对试验/错误和逆向工程的需求,需要的时间比我能投入的更多,所以我很遗憾地恢复使用字符串。

如果有人可以指出在线指导信息,我将不胜感激。

4

1 回答 1

1

只是部分答案......

以下测试表明分类数据在用作连接键时表现良好:

BigList = {'dog' 'cat' 'mouse' 'horse' 'rat'}'
SmallList = BigList( 1 : end-2 )

Nrows = 20;

% Create tables for innerjoin using strings

tBig = table( ...
    (1:Nrows)' , ...
    BigList( ceil( length(BigList) * rand( Nrows , 1 ) ) ) , ...
    'VariableNames' , {'B_ID' 'Animal'} )

tSmall = table( ...
    (1:Nrows)' , ...
    SmallList( ceil( length(SmallList) * rand( Nrows , 1 ) ) ) , ...
    'VariableNames' , {'S_ID' 'Animal'} )

tBigSmall = innerjoin( tBig , tSmall , 'Keys','Animal' );
tBig = sortrows( tBig , {'Animal','B_ID'} );
tSmall = sortrows( tSmall, {'Animal','S_ID'} );
tBigSmall = sortrows( tBigSmall, {'Animal' 'B_ID' 'S_ID'} );

% Now innerjoin the same tables using categorized strings

tcBig = tBig;
tcBig.cAnimal = categorical( tcBig.Animal );
tcBig.Animal = [];

tcSmall = tSmall;
tcSmall.cAnimal = categorical( tcSmall.Animal );
tcSmall.Animal = [];

tcBigSmall = innerjoin( tcBig , tcSmall , 'Keys','cAnimal' );
tcBig = sortrows( tcBig , {'cAnimal','B_ID'} );
tcSmall = sortrows( tcSmall, {'cAnimal','S_ID'} );
tcBigSmall = sortrows( tcBigSmall, {'cAnimal' 'B_ID' 'S_ID'} );

% Check if the join results are the same

if all( tBigSmall.Animal == tcBigSmall.cAnimal )
    disp('categorical vs string key: inner joins MATCH.')
else
    disp('categorical vs string key: inner joins DO NOT MATCH.')
end % if

所以现在唯一的问题是速度。这是一个普遍的问题,不仅仅是连接,所以我不确定什么是一个好的测试。有很多可能性,例如表行数、类别数、是连接还是过滤等。

无论如何,我相信这两个问题的答案都会得到更好的记录。

于 2018-09-17T20:19:36.750 回答