MATLAB:SQL风格的联接操作 [英] MATLAB: SQL-style join operation
本文介绍了MATLAB:SQL风格的联接操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想用Matlab做一个SQL风格的联接.我可以通过创建数据集(统计工具箱),进行联接,转换回并再次删除数据集来做到这一点:
I would like to do an SQL-style join with Matlab. I could do it by creating datasets (stats toolbox), doing the join, converting back and deleting the datasets again:
A1 = [10 10 20 20 30 30 50 50]';
B1 = (10:10:70)';
B2 = (1:7)';
dsA = dataset({A1, 'Key'});
dsB = dataset({B1, 'Key'}, {B2, 'Idx'});
dsA = join(dsA, dsB, 'key', 'Key');
结果:
Key Idx
10 1
10 1
20 2
20 2
30 3
30 3
50 5
50 5
我的问题:是否还有另一种方法,不使用数据集并且不使用for循环?
My Question: Is there another way of doing this, without using datasets and without for loops?
非常感谢您的帮助!
推荐答案
在您的示例中,您只需使用 ISMEMBER :
For your example, you could simply use ISMEMBER:
[~,loc] = ismember(A1,B1);
dsA = [B1(loc) B2(loc)]
结果:
dsA =
10 1
10 1
20 2
20 2
30 3
30 3
50 5
50 5
注意:这假定A1
中的所有元素都在B1
中找到.如果不是总是这样,则可以使用ISMEMBER的第一个输出来过滤loc
...
Note: this assumes that all the elements in A1
are found in B1
. If that not always the case, you could use the first output of ISMEMBER to filter the zero-values in loc
...
这篇关于MATLAB:SQL风格的联接操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文