Code covered by the BSD License  

Highlights from
Relational Join

4.0

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

Relational Join

by Stefan Schroedl

 

31 Mar 2008 (Updated 01 Apr 2008)

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  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (4)
29 May 2011 Dmitriy  
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

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 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)

Please login to add a comment or rating.
Tag Activity for this File
Tag Applied By Date/Time
matrices Stefan Schroedl 22 Oct 2008 09:55:52
relational algebra Stefan Schroedl 22 Oct 2008 09:55:52
set Stefan Schroedl 22 Oct 2008 09:55:52
join Stefan Schroedl 22 Oct 2008 09:55:52
relational algebra gg 23 Jul 2010 03:57:50
join Ali GutiƩrrez 19 Sep 2010 17:59:49

Contact us at files@mathworks.com