File Exchange

## Relational Join

version 1.0 (3.52 KB) by

Relational Join of Two Matrices

Updated

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.

Dmitriy

### Dmitriy (view profile)

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)

Dmitriy

### Dmitriy (view profile)

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

Dmitriy

### Dmitriy (view profile)

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

Dmitriy

### Dmitriy (view profile)

##### MATLAB Release
MATLAB 7.4 (R2007a)
##### Tags Add Tags

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.