Code covered by the BSD License  

4.0

4.0 | 1 rating Rate this file 8 Downloads (last 30 days) File Size: 3.52 KB File ID: #19402

Relational Join

by

 

31 Mar 2008 (Updated )

Relational Join of Two Matrices

| Watch this File

File Information
Description

Example:
A = [ 3 4 5; 1 2 3; 6 7 8];
B = [ 9 10 11; 3 4 55; 6 7 88];
join(A,1:2,B,1:2)

ans =
     3 4 5 55
     6 7 8 88

Join options (inner, outer left, right, full) supported

Matlab contains a number of set-oriented function like INTERSECT, UNIQUE, UNION, SETDIFF, SETXOR, ISMEMBER. However, one interesting missing function is something similar to a database-like JOIN. This can be written using above functions, but requires a for-loop which makes it very slow for large datasets.

We supply a C++- mex file and a wrapper matlab function.

MATLAB release MATLAB 7.4 (R2007a)
Other requirements Compilation tested on Linux
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (4)
29 May 2011 Dmitriy

I think replacing
C(ia(ib>0), ((numColsA+1):end)) = B(idx, colValB); %line 65
with
C(ib>0, ((numColsA+1):end)) = B(idx, colValB);
will fix left-join problem in the prev post
(adjustment for right-join is similar)

29 May 2011 Dmitriy

also I don't like the output for this example
mjoin([1 2;1 4],1,[1 3; 1 8],1,'l')
ans =
1 2 8
1 2 8
1 4 NaN
1 4 NaN

29 May 2011 Dmitriy

I use this function a lot (2009a).
Pros: mex
Cons: 1) doesn't preserve initial order. Eg.
tmp=mjoin([BM(:,3) (1:size(BM,1))'],1,BM2,1,'l');
BM(tmp(:,2),tmp(:,3:end)); issorted(tmp(:,2)) is false unless BM is sorted by 3d column

2) If while compiling mex unix says "can't find strcmp" - including "#include <stdio.h>
#include <string.h>" solves the problem

29 May 2011 Dmitriy  

Contact us